Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add watch on a non scope variable in angularjs

Tags:

Is there a way to add watch to a non scope variable. I want to add a watch to local variable. I have something like this

 function EditAssetRegistryController(assetregistryService, manufacturerService, assettypeService, projectService, $localStorage, $routeParams) {         var vm = this;         vm.manufacturers = [];         vm.projects = [];         vm.asset_types = [];         vm.ch_group_uniq = 'none'; } 

here is there a way to add watch to vm.ch_group_uniq? I know how it will be done with scope variable but I have scenarios where I have to check many complex variables.

like image 326
Adnan Ali Avatar asked Apr 15 '15 06:04

Adnan Ali


People also ask

How to watch changes in $scope in AngularJS?

In angularjs $watch () function is used to watch the changes of variables in $scope object. Generally the $watch () function will create internally in angularjs to handle variable changes in application.

What is the use of $watch in AngularJS?

In angularjs $watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in angularjs to handle variable changes in application. Suppose in our angularjs applications if we want to create custom watch for some actions then it’s better to use $scope.watch function.

How to check if the value has changed in AngularJS?

The value function should return the value which is being watched. AngularJS can then check the value returned against the value the watch function returned the last time. That way AngularJS can determine if the value has changed.

How to watch changes in scope variables of a Watcher?

Once a watcher is configured with the scope, the listener function is called to initialize the watcher. This method is for watching changes of scope variables. This method has callback function which gets called when the watching properties are changed.


Video Answer


1 Answers

Well, you can easily add a watch for anything by passing a function as the first parameter:

$scope.$watch(function watchFunction(scope) {     return vm.ch_group_uniq }, handler) 

A few things to consider: watchFunction must return the same value if nothing has changed. This can lead to some gotchas, for example, returning the result of some array operations: [1,2,3].filter(...) will always return a new array, and lead to an endless $digest cycle. Also note the third parameter of $scope.$watch, which indicates whether to use an identity comparison, or angular.equals when comparing the values. (Check out the docs for further information - https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch)

However, your specific problem seems to be trying to use controllerAs and custom $watch-es. There's a very handy library that addresses this issue specifically: https://github.com/christopherthielen/angular-360-no-scope

like image 85
Sacho Avatar answered Sep 19 '22 23:09

Sacho