【发布时间】:2015-07-18 15:07:47
【问题描述】:
我想通过它的 id 更新元素的属性,并让元素对这个变化做出反应。
我试图创建一个plunkr 来反映我的情况,但我似乎连 ng-click 都无法工作。
但是,我想做的是调用一个执行以下操作的函数
var cell = angular.element(document.querySelector('#' + cellName));
cell.attr('card-id', id);
这似乎可行,根据我可以在这些行之后直接再次读出的值。
接收元素如下所示:
deckbuilderApp.directive('card', function() {
return {
restrict: 'E',
replace: true,
templateUrl: 'tpl/deckCard.tpl.html',
scope: {
cardId: '=cardId'
},
link: function(scope, element, attrs) {
attrs.$observe('cardId', function(newId) {
console.log('observe cardId: new Value: ' + newId);
console.log('observe cardId: id: ' + scope.id + ' scope:' + scope);
});
scope.$watch('cardId', function(oldValue, newValue) {
console.log('$watch cardId: ' + oldValue + ' -> ' + newValue);
});
attrs.$observe(function() { return attrs.cardId }, function(newId) {
console.log('ssssobserve card-id: new Value: ' + newId);
console.log('sssssobserve card-id: id: ' + scope.id + ' scope:' + scope);
});
scope.$watch(function() { return attrs.cardId }, function(oldValue, newValue) {
console.log('sssssssssss$watch card-id: ' + oldValue + ' -> ' + newValue);
});
console.log('linked card directive');
}
}
});
当我通过 ng-repeat 设置值时,会在初始化时调用 watch 和 observer 函数(“ssssobserver”除外)。
对 card-id 属性的后续更改不会触发 watch 或 observer 代码。
我需要做什么才能让它工作?
【问题讨论】:
标签: javascript angularjs