在 angularjs 中的非作用域变量上添加监视 [英] add watch on a non scope variable in angularjs
本文介绍了在 angularjs 中的非作用域变量上添加监视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法将监视添加到非作用域变量.我想向局部变量添加一个手表.我有这样的东西
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';
}
这里有一种方法可以将监视添加到 vm.ch_group_uniq 吗?我知道如何使用作用域变量来完成,但我有一些场景,我必须检查许多复杂的变量.
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.
推荐答案
$watch
将无法像使用 controllerAs
的正常语法一样工作.你需要把它绑定到$scope
,然后你就可以观察那个变量了:
$watch
will not work as normal syntax with controllerAs
. You need to bind it to $scope
, and then you can watch that variable:
代码
$scope.$watch(angular.bind(this, function (ch_group_uniq) {
return this.ch_group_uniq;
}), function (newVal, oldVal) {
console.log('Name changed to ' + newVal);
});
这是参考托德格言文章
这篇关于在 angularjs 中的非作用域变量上添加监视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文