I am a bit confused about how to "redraw" a directive once one of the values that I pass to it has been updated.
一旦我传递给它的一个值被更新,我对如何“重绘”一个指令感到有点困惑。
What I have is a directive that draws a chart depending on a configuration (a JSON) that I pass to it as a scope parameter. In the controller of the view that contains the directive I can update this configuration and I want to update the directive when the new configuration is updated by the user.
我所拥有的是一个指令,它根据我作为范围参数传递给它的配置(JSON)绘制图表。在包含该指令的视图的控制器中,我可以更新此配置,并且我希望在用户更新新配置时更新该指令。
The configuration JSON looks like:
配置JSON如下所示:
$scope.configuration = {
widget: {
range: {
maximum: 0,
minimum: -20
}
}
}
And the directive looks like:
该指令看起来像:
<my-chart configuration configuration="configuration"></my-chart>
So when I update the configuration I want the directive to refresh the content (like initialise it again).
因此,当我更新配置时,我希望指令刷新内容(如再次初始化)。
1 个解决方案
#1
2
You have to use $scope.$watch, or $scope.$watchCollection(). Here is the link
你必须使用$ scope。$ watch或$ scope。$ watchCollection()。链接在这里
Your code can be like this
你的代码可以是这样的
$scope.$watchCollection('$scope.configuration.widget.range', function reInit(values){
init(values)
});
$watchCollection(obj, listener); Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). If a change is detected, the listener callback is fired.
$ watchCollection(obj,listener); Shamp监视对象的属性,并在任何属性发生更改时触发(对于数组,这意味着要观察数组项;对于对象映射,这意味着要观察属性)。如果检测到更改,则会触发侦听器回调。
#1
2
You have to use $scope.$watch, or $scope.$watchCollection(). Here is the link
你必须使用$ scope。$ watch或$ scope。$ watchCollection()。链接在这里
Your code can be like this
你的代码可以是这样的
$scope.$watchCollection('$scope.configuration.widget.range', function reInit(values){
init(values)
});
$watchCollection(obj, listener); Shallow watches the properties of an object and fires whenever any of the properties change (for arrays, this implies watching the array items; for object maps, this implies watching the properties). If a change is detected, the listener callback is fired.
$ watchCollection(obj,listener); Shamp监视对象的属性,并在任何属性发生更改时触发(对于数组,这意味着要观察数组项;对于对象映射,这意味着要观察属性)。如果检测到更改,则会触发侦听器回调。