加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Angular Controller As Syntax-no method’$watchC

发布时间:2020-12-17 07:09:01 所属栏目:安全 来源:网络整理
导读:我试图在Angular中将Controller作为语法.在这一点上,我在我的routerProvider中有它…不确定它对我遇到的问题是否重要,但在这里它仍然是: angular.module('ucp.kick',[ 'ngRoute']).config ($routeProvider,APP_BASE_URL) - $routeProvider .when APP_BASE_U
我试图在Angular中将Controller作为语法.在这一点上,我在我的routerProvider中有它…不确定它对我遇到的问题是否重要,但在这里它仍然是:

angular.module('ucp.kick',[
  'ngRoute'
])
.config ($routeProvider,APP_BASE_URL) ->
  $routeProvider
  .when APP_BASE_URL + 'kicks',name: 'Kicks'
    templateUrl: 'kick/partials/kick.html'
    controller: 'kick as KickController'

这是我的控制器的浓缩版本:

this.$watchCollection('filtered.devices',function(devices) {
    return this.filteredDevices = devices;
  });

但我得到:

TypeError: Object [object Object] has no method '$watchCollection'

我意识到当使用控制器作为语法时,您不希望注入范围.那么,我如何访问$watchCollection函数?

解决方法

你仍然需要注入$scope才能使用$watch和$watchCollection.现在,你会认为你可以这样做:

$scope.$watchCollection('filtered.devices',function (newVal,oldVal) {});

要么

$scope.$watchCollection('this.filtered.devices',oldVal) {});

但那不行.所以你需要做:

var that = this;
$scope.$watchCollection(function () {
    return that.filtered.devices;
},oldVal) {

});

要么:

$scope.$watchCollection(angular.bind(this,function () {
    return this.filtered.devices;
}),oldVal) {

});

要么:

$scope.$watchCollection("KickController.filtered.devices",function(newVal,oldVal) { });

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读