Unbinding $watch in angularjs after called
you can just do it the same way you already do, call the "deregistration" inside your function:
var unbind = $scope.$watch("tag", function () { // ... unbind();});
Because tag
is an expression, you can use one-time binding to unbind once the value was received:
$scope.$watch("::tag", function () {});
angular.module('app', []).controller('example', function($scope, $interval) { $scope.tag = undefined $scope.$watch('::tag', function(tag) { $scope.tagAsSeen = tag }) $interval(function() { $scope.tag = angular.isDefined($scope.tag) ? $scope.tag + 1 : 1 }, 1000)})angular.bootstrap(document, ['app'])
<html><head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title></head><body ng-controller="example">Tag: {{tag}}<br>Watch once result: {{tagAsSeen}}</body></html>
bindonce directive maybe what you need.