4

Why is the one time binding called twice?

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
    $scope.foo = function() {
        console.log('foo'); 
        return 'foo';
    }
});
<!DOCTYPE html>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <body>
        <div ng-app="myApp" ng-controller="myCtrl">
            <span ng-bind="::foo()"></span>
        </div>
    </body>
</html>

The same is for the regular binding (during first digest).

7
  • 3
    One time binding doesn't mean the function won't be evaluated every digest. If you don't want it called then run it in controller once and set a variable to pass to view instead Commented Jul 31, 2017 at 11:27
  • Agree with you. I can be replaced with preliminary initialization. But it is not useful from general perspective if there are a lot of one time binding depend on some logic. Commented Jul 31, 2017 at 11:44
  • Ok but what problem does it create? Commented Jul 31, 2017 at 11:48
  • It's about performance Commented Jul 31, 2017 at 11:48
  • The performance gain comes from less watchers. You can't control how internals handle the digests Commented Jul 31, 2017 at 11:56

2 Answers 2

2

This is an issue in angular core, read this post where they have a detailed discussion on this issue, They have explained one time bindings are not the way we think, expression evaluation can be called multiple times.

Sign up to request clarification or add additional context in comments.

Comments

-1

The binding function will be called at every digest even if it is one time binding. If you want to avoid this then use NgInit to call function once.

var app = angular.module('myApp', []);

app.controller('myCtrl', function($scope) {
    $scope.foo = function() {
        console.log('foo'); 
        return 'foo';
    }
});
<!DOCTYPE html>
<html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
    <body>
        <div ng-app="myApp" ng-controller="myCtrl">
            <span ng-init="::foo()"></span>
        </div>
    </body>
</html>

1 Comment

The binding function will not be called at every digest if it is one time binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.