M
M
Maxim Ivanov2016-10-19 16:50:50
Angular
Maxim Ivanov, 2016-10-19 16:50:50

How to determine why a function is called twice in a controller?

I have a controller:

<div ng-controller="Main">
<widget autoupdate="true" exec="myconsole"/>
<widget />
<widget />
<widget />
<widget />
<widge/>

</div>

function Main($scope){
$scope.myconsole = function(val) {
console.log(val);
}


}

Create a widget component
const widgetsComponent = {

   bindings: {
  
     'autoupdate': '@',
     'exec': '=', 
    },

    template: require('./view/blocks/widgets.html'),
};

Widget Code
<div>
 {{ $ctrl.autoupdate ? $ctrl.exec(1) : 0  }}

..
</div>

As a result:
a26d9f43a02140ad993a38aef1d17f82.png
I mean, if you write exec for components 2 times, then it will be executed even more, why?
<div ng-controller="Main">
<widget autoupdate="true" exec="myconsole"/>
<widget />
<widget />
<widget />
<widget autoupdate="true" exec="myconsole"/>

</div>

9b1b3d5cae5e436a841e4b245d74a4e9.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-10-19
@splincodewd

Because {{ $ctrl.autoupdate ? $ctrl.exec(1) : 0 }} will be executed on each sneeze of the $digest loop.

M
Michael, 2016-10-19
@springimport

<widget autoupdate="true" exec="myconsole"/>
...
<widget autoupdate="true" exec="myconsole"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question