角控制器作为语法,没有一种方法'$ watchCollection“ [英] Angular Controller As Syntax-no method '$watchCollection'

查看:127
本文介绍了角控制器作为语法,没有一种方法'$ watchCollection“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在角做控制器语法。在这一点上,我有它在我的routerProvider ......不知道是否会事宜因为我遇到的问题,但在这里反正是:

I am trying to do Controller As Syntax in Angular. At this point, I have it in my routerProvider...not sure if it would matter for the issue I am having, but here it is anyways:

 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'

下面是我的控制器的精简版我有:

Here is a condensed version of my controller I have:

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

但我得到:

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

使用控制器作为语法时,我知道,你不希望注入的范围。那么,如何访问 $ watchCollection 功能?

推荐答案

您仍然需要注入 $范围为了使用 $手表 $ watchCollection 。现在,你可能会认为你可以这样做:

You will still need to inject $scope in order to use $watch and $watchCollection. Now, you would think you could do:

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

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

但是,这是行不通的。所以,你需要做的两种:

But that won't work. So you need to either do:

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

});

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

});

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

这篇关于角控制器作为语法,没有一种方法'$ watchCollection“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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