在指令$腕表功能角JS没有得到触发 [英] $watch function in directive not getting triggered in Angular JS
问题描述
我是新来的角js和已经到处找一个答案,为什么这是行不通的。
I'm new to angular js and have been looking everywhere for an answer to why this isn't working.
我有我的指示在这里:
.directive('carrouselPreview', function () {
return function (scope, element, attrs) {
scope.$watch(scope.carouselPreview, function () {
alert('changed');
}, true);
}
});
这手表更改scope.carousel preVIEW,它是通过此功能更改:
This watches for a change to scope.carouselPreview, which is changed through this function:
$scope.showPreview = function(ind){
$scope.carouselPreview = ind;
}
,此功能无疑火灾和变化scope.carousel preVIEW - !但手表的功能永远不会触发
This function definitely fires and changes scope.carouselPreview - but the watch function never fires!
我意识到我可能是愚蠢的,但在这里我已经看了遍,似乎完全没有问题。
I realise I'm probably being dumb here but I've looked all over and it seems perfectly fine.
如果有人可以帮助我解决这个问题,今晚我会永远爱你!
If anyone helps me fix this tonight I'll love you forever!
推荐答案
第一个参数到 $观看
是一个字符串(在范围内的上下文中计算)或的功能。所以,你想要的是:
The first parameter into the $watch
is a string (evaluated in the context of the scope) or a function. So what you want is:
scope.$watch("carouselPreview", function () {
alert('changed');
}, true);
请参阅http://docs.angularjs.org/api/ng.$rootScope.Scope#$watch
这篇关于在指令$腕表功能角JS没有得到触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!