javascript - AngularJS Scope variable Unwatch -
i want unwatch 1 angular scope variable after point in program.
here following.
$scope.oldvar = {data: "something"} $scope.blankarray = [] $scope.blankarray.push($scope.oldvar) now whatever changes doing $scope.oldvar, reflecting changes blankarray viewing on screen.
is there way in angular restrict that?
use angular.copy
you can use angular.copy create new copy of data. prevent data being bound within blankarray.
so @ point in application, unbind values, can use angular.copy.
$scope.blankarray = angular.copy($scope.blankarray) here dragons
the problem unbinding value you'll introducing complexity controller needs aware of when value bound , when isn't bound. introduces state manage , logic needs have awareness.
demonstration
function ctrl($scope) { $scope.oldvar = {data: "something"}; $scope.blankarray = []; $scope.blankarray.push($scope.oldvar); $scope.withoutcopy = [] $scope.withoutcopy.push($scope.oldvar); $scope.change = function() { $scope.oldvar.data = math.random(); }; $scope.unbind = function() { $scope.blankarray = angular.copy($scope.blankarray); }; }; <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app ng-controller="ctrl"> <button ng-click="change()">test</button> <button ng-click="unbind()">unbind</button> <pre>{{ blankarray | json }}</pre> <pre>{{ oldvar | json }}</pre> <pre>{{ withoutcopy | json }}</pre> </div>
Comments
Post a Comment