问题描述
我有一个像这样初始化的角度指令:
I have an angular directive which is initialized like so:
<conversation style="height:300px" type="convo" type-id="{{some_prop}}"></conversation>
我希望它足够智能以在 $scope.some_prop
更改时刷新指令,因为这意味着它应该显示完全不同的内容.
I'd like it to be smart enough to refresh the directive when $scope.some_prop
changes, as that implies it should show completely different content.
我已经按原样对其进行了测试,但没有任何反应,当 $scope.some_prop
更改时,甚至不会调用链接函数.有没有办法做到这一点?
I have tested it as it is and nothing happens, the linking function doesn't even get called when $scope.some_prop
changes. Is there a way to make this happen ?
推荐答案
Link 函数只会被调用一次,因此它不会直接执行您期望的操作.您需要使用 angular $watch
来观察模型变量.
Link function only gets called once, so it would not directly do what you are expecting. You need to use angular $watch
to watch a model variable.
此手表需要在链接功能中设置.
This watch needs to be setup in the link function.
如果你对指令使用隔离范围,那么范围将是
If you use isolated scope for directive then the scope would be
scope :{typeId:'@' }
在您的链接功能中,然后您添加一个手表
In your link function then you add a watch like
link: function(scope, element, attrs) {
scope.$watch("typeId",function(newValue,oldValue) {
//This gets called when data changes.
});
}
如果您不使用隔离范围,请在 some_prop
If you are not using isolated scope use watch on some_prop
这篇关于参数更改时的角度指令刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!