angularjs - angular : duplicate in calling controller's function -
i have following angular code
<!doctype html> <html> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <body> <div ng-app="myapp" ng-controller="myctrl"> test : {{mytest()}} </div> <script> var app = angular.module('myapp', []); app.controller('myctrl', function($scope) { $scope.name= "john "; $scope.mytest = function () { console.log('my test'); return 'something'; }; }); </script> </body> </html>
for more detail, please refer http://plnkr.co/edit/uiu50aolmwkijnaphib5
problem: when view in chrome browser 'inspect element' console, function 'my test' called 3 times! why ?
because you've asked angular that. expression
{{mytest()}}
is in fact instruction:
"angular, check if result value of
mytest()
not changing. , regularly".
and angular checking few times, sure not changed. , later, in other digest same again
so, rather trigger method once, , let angular watch resulting expression, name
above
{{name}}
Comments
Post a Comment