我想在单击指令时更改类(内部指令),这是我当前的代码,scope.myattr
在控制台中更新但不在模板或视图中:
<test order="A">Test</test>
.directive("test", function () {
return {
restrict: 'E',
scope: {
myattr: "="
},
transclude: true,
link: function (scope, element, attrs) {
scope.myattr = attrs.myattr;
element.bind('click', function () {
console.log(scope.myattr);
if (scope.myattr == 'A') {
scope.myattr = 'B';
}
else {
scope.myattr = 'A';
}
});
},
template: `
<span ng-transclude></span>
<span class="sortIcon {{myattr}}">{{myattr}}</span>`
}
});
您的问题是关于使用$apply
方法消化循环执行。
替换这一行: scope.myattr = attrs.myattr;
这样:
scope.$apply(function(){
scope.myattr = attrs.myattr;
});
这会手动强制在作用域上发生摘要循环,该循环将遍历其所有观察者并查找更改。
请记住在该范围内进行所有更新。
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句