Angularjs 角度控制器作为语法无方法'$手表收藏&x27;
我试图在Angular中使用Controller作为语法。在这一点上,我的routerProvider中有它…不确定它是否对我的问题有影响,但无论如何,它在这里:Angularjs 角度控制器作为语法无方法'$手表收藏&x27;,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我试图在Angular中使用Controller作为语法。在这一点上,我的routerProvider中有它…不确定它是否对我的问题有影响,但无论如何,它在这里: angular.module('ucp.kick', [ 'ngRoute' ]) .config ($routeProvider, APP_BASE_URL) -> $routeProvider .when APP_BASE_URL + 'kicks', name: 'Kicks' templat
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
在将$scope
注入控制器后,正如在出现controllerAs
语法之前一样,您可以执行:$scope.$watchCollection(…)
进一步重复我的意思:如果您试图查看的值使用controllerAs
语法绑定到一个值,例如kickController.someValue
,那么您将查看该值$scope.$watchCollection('kickController.someValue',…)
旁注:显示所有代码会有所帮助,因为看起来您可以将控制器定义向后。你的意思是:
。。。
控制器:KickController作为kick
然后:
var kick=this代码>
?控制器作为
不会用$scope
替换控制器。控制器没有$watchCollection
属性。您仍然必须注入$scope
并使用$scope.$watchCollection
,并且必须使用控制器标识符作为要监视的集合的名称
$scope.$watchCollection("KickController.filtered.devices"
也就是说,通常可以避免使用$watch
和$watchCollection
,您只需使用数据绑定即可。Angular将在集合内部使用$watchCollection
。具体而言,我不确定您是否需要此其他属性filteredevices
,因为您可以在现有集合上显式使用筛选器。您仍然需要注入$scope
才能使用$watch
和$watchCollection
。现在,你会认为你可以做到:
$scope.$watchCollection('filtered.devices', function (newVal, oldVal) {});
或
但那是行不通的。因此,您需要:
var that = this;
$scope.$watchCollection(function () {
return that.filtered.devices;
}, function (newVal, oldVal) {
});
或:
或:
迷人的!关于那件事从来都不是新鲜事
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) { });