0

I've looked through source code but haven't found relative piece of code. I'm particularly interested in bind-once watchers and the way to attach them. At the moment I've done the following:

var removeNodeTypeWatcher = scope.$watch(function () {
    return paneController.getNodeType();
}, function (value) {
    if (value) {
        scope.nodeType = value;
        // as soon as the value is defined we no longer need watcher
        removeNodeTypeWatcher();
    }
});
3
  • is getNodeType asynchronous? Otherwise, do you even need a watch? Commented Feb 18, 2015 at 8:24
  • good point, but the question is not about that :) let's assume that it is Commented Feb 18, 2015 at 8:28
  • bindoncedoes the same, unregister watch when value is resolved Commented Feb 18, 2015 at 8:46

1 Answer 1

2

You can use the specific expression

::

like so:

<div>{{::item}}</div>

or

<ul>  
   <li ng-repeat="item in ::items">{{item}}</li>
</ul>

to bind expression once since angular 1.3: bind once since 1.3

Also, there are specific libraries like:

angular-once or bindonce

EDIT:

I found the following function in the source of angularjs 1.3:

function oneTimeWatch(scope, listener, objectEquality, deregisterNotifier, parsedExpression) {
    var unwatch, lastValue;
    return unwatch = scope.$watch(function oneTimeWatch(scope) {
        return parsedExpression(scope);
    }, function oneTimeListener(value, old, scope) {
        lastValue = value;
        if (isFunction(listener)) {
            listener.apply(this, arguments);
        }
        if (isDefined(value)) {
            scope.$$postDigest(function() {
                if (isDefined(lastValue)) {
                    unwatch();
                }
            });
        }
    }, objectEquality, deregisterNotifier);
}

It is located at src/ng/parse.js, starting at line 1031:oneTimeWatch

However, in 1.4, the function is called

oneTimeWatchDelegate

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

8 Comments

And if you look at the source of bindonce you can see they use the same approach as Maximus
@Manube, thanks, but I was asking about how this is implemented inside angular.
@DieterGoetelen, can you point me to the source code line with bind once?
@Maximus awch, I was pointing to angular-once, source: github.com/tadeuszwojcik/angular-once/blob/master/once.js#L22
oops, sorry, had misinterpreted. Indeed the relative code seems well hidden, will keep looking...
|

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.