参数变更指令角刷新 [英] Angular Directive refresh on parameter change
问题描述
我有的像这样初始化一个角度指令:
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功能只被调用一次,所以它不会直接做你期待什么。您需要使用角 $观看
来观看模型变量。
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
范围:{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
这篇关于参数变更指令角刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!