在angularjs中的非范围变量上添加监视 [英] add watch on a non scope variable in angularjs

查看:64
本文介绍了在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 将不能像 controlsAs 一样正常语法。您需要将其绑定到 $ 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);
});

这是参考资料 Todd Motto文章

Here is the reference Todd Motto Article

这篇关于在angularjs中的非范围变量上添加监视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆