尝试使用ng-class
.
<input type="radio" ng-class="slot.class" />
观察者仍然是绑定的,但是每次摘要发生时都不会设置类属性,只有当值发生slot.class
变化时。
编辑:更新以反映原始海报想要的内容。
我不确定这是否是一个好的做法,但您可以尝试编写一个指令,通过取消注册所有观察者来生成一个“哑”模板。这样,只有当 watch 语句的结果发生变化时,您才会更新模板。
像这样的东西:
module.directive('shallowWatch', function($compile){
return {
compile : function(tElement, tAttrs){
var templateFN = $compile(tElement.html());
tElement.empty();
return function(scope, element, attrs){
var childScope;
scope.watch(attrs.shallowWatch, function(){
element.empty();
if(childScope){
childScope.$destroy();
}
childScope = scope.$new();
element.append(templateFn(childScope));
traverseScope(childScope, function(scope){
scope.$$watchers = [];
});
});
};
}
};
function traverseScope(target, fn){
var current, next;
current = target;
do {
fn.apply(current, [current]);
//depth-first traversal pulled from angularjs core
if (!(next = (current.$$childHead || (current !== target && current.$$nextSibling)))) {
while(current !== target && !(next = current.$$nextSibling)) {
current = current.$parent;
}
}
} while ((current = next));
}
});
你会像这样使用它:
<div shallow-watch="days">
<div ng-repeat"day in days">
<label ng-repeat="slot in day.slots">
<input type="radio" class="{{slot.class}}" value="{{slot.value}}" ng-disabled="{{slot.disabled}}">
</label>
</div>
</div>