angularjs:强制重新渲染/完全刷新指令模板

时间:2022-11-21 20:09:08

This is a rewriting because of the off-topic issue:

由于题外话题,这是重写:

I wrote a directive for a dropdown. I am using the directive two times as kind of a workflow. the first dropdown triggers by change action the refresh of the model of the second one. though the model updates correctly, the view does not. the many times you select items in dropdown one the second dropdown shows old items with new items, eventhough the console.log() of the angular model is correct.

我为下拉列表写了一个指令。我使用该指令两次作为工作流程。第一个下拉列表通过更改操作触发第二个模型的刷新。虽然模型正确更新,但视图却没有。很多时候你在下拉列表中选择项目,第二个下拉列表显示带有新项目的旧项目,尽管角度模型的console.log()是正确的。

the view has infinite recursion. Using ng-include with an id of a ng/template that does recursion about it self:

视图具有无限递归。使用ng-include与ng /模板的id进行自我递归:

my initial plunkr showing the template code is this: http://plnkr.co/edit/jera17uCluGsFs8o5hRG?p=preview

我的初始plunkr显示模板代码是这样的:http://plnkr.co/edit/jera17uCluGsFs8o5hRG?p = preview

I did not understand why the view does not fully refresh?

我不明白为什么视图没有完全刷新?

to be updated.

要被更新。

kind regards, alex

亲切的问候,亚历克斯

1 个解决方案

#1


36  

Typically what I do is use ui-if directive which is basically an if statement made by angularui team so I'll put it on the element that is also the directive and say something like:

通常我所做的是使用ui-if指令,它基本上是由angularui团队制作的if语句,因此我将它放在也是指令的元素上,并说出如下内容:

<div ui-if="mydata.prop" my-custom-directive></div>

This way if myData.prop changes or I set it to null briefly then reapply it it will remove and then transclude my html directive back into the DOM causing it to eval my directive again.

这样,如果myData.prop更改或我将其简单地设置为null然后重新应用它将删除然后将我的html指令转换回DOM,导致它再次评估我的指令。

#1


36  

Typically what I do is use ui-if directive which is basically an if statement made by angularui team so I'll put it on the element that is also the directive and say something like:

通常我所做的是使用ui-if指令,它基本上是由angularui团队制作的if语句,因此我将它放在也是指令的元素上,并说出如下内容:

<div ui-if="mydata.prop" my-custom-directive></div>

This way if myData.prop changes or I set it to null briefly then reapply it it will remove and then transclude my html directive back into the DOM causing it to eval my directive again.

这样,如果myData.prop更改或我将其简单地设置为null然后重新应用它将删除然后将我的html指令转换回DOM,导致它再次评估我的指令。