简体   繁体   English

如何强制更新ng-init?

[英]How to force update of ng-init ?

I found that ng-init is not updated after rowIndex is updated. 我发现在更新rowIndex之后不会更新ng-init。

<div ng-init="item = getItem(rowIndex)">
   {{ item.Name }} 
</div>

item is not updated after rowIndex is changed (even if changes are made with $timeout) rowIndex更改后,项目不会更新(即使使用$ timeout进行了更改)

Is there other directive that allows to avoid using the following dirty trick: 还有其他指令可以避免使用以下肮脏的把戏:

<div ng-repeat="item in [getItem(rowIndex)]">
   {{ item.Name }} 
</div>

ngInit does not update. ngInit不更新。 It's a one-shot directive. 这是一个一次性指令。 You should just call the function from within the curly braces, since you are wanting to watch for changes to rowIndex : 您只需要在花括号内调用该函数,因为您要注意对rowIndex更改:

<div>
  {{ getItem(rowIndex).Name }}
</div>

This could hurt performance on large lists, since getItem will be called on every digest cycle, but it's easy to read. 这可能会损害大型列表的性能,因为在每个摘要周期都会调用getItem ,但它很容易阅读。

https://plnkr.co/edit/cZ2SG6nf2SufdAWe1ye7?p=preview https://plnkr.co/edit/cZ2SG6nf2SufdAWe1ye7?p=preview

Another approach is to set the variable inside a bogus attribute: 另一种方法是在伪属性内设置变量:

<div x="{{item = getItem(rowIndex)}}">
    {{ item.Name }} {{item.Value}}
</div>

On each digest cycle the value of item will be updated. 在每个摘要循环中, item的值将被更新。

In this case where item has more that one property, the getItem function needs to be invoked only once. item具有多个属性的情况下,只需调用一次getItem函数。

Keep in mind that it would be even more efficient for the controller to update the value of item only once when rowIndex changes instead of invoking the getItem function every digest cycle. 请记住,当rowIndex更改时,控制器仅一次更新item的值将比在每个摘要周期调用getItem函数更有效。


Avoid using ng-init 避免使用ng-init

Avoid using ng-init . 避免使用ng-init It tangles the Model and View, making code difficult to understand, test, debug, and maintain. 它缠结了模型和视图,使代码难以理解,测试,调试和维护。 Instead initialize the Model in the controller. 而是在控制器中初始化Model。

From the Docs: 从文档中:

ngInit ngInit

This directive can be abused to add unnecessary amounts of logic into your templates. 可以滥用此指令在模板中添加不必要的逻辑量。 There are only a few appropriate uses of ngInit , such as for aliasing special properties of ngRepeat , as seen in the demo below; ngInit只有少数适​​当的ngInit ,例如别名ngRepeat特殊属性,如下面的演示所示; and for injecting data via server side scripting. 以及通过服务器端脚本注入数据。 Besides these few cases, you should use controllers rather than ngInit to initialize values on a scope. 除了这几种情况,您应该使用控制器而不是ngInit来初始化作用域上的值。

— AngularJS ng-init Directive API Reference — AngularJS ng-init指令API参考

Based off your question title I'd say this article could be another good resource to go over for you. 基于您的问题标题,我想说这篇文章可能是您的另一篇不错的参考资料。 Angular has a pretty good variety of lifecycle hooks (which is all ngInit is). Angular有很多生命周期挂钩(这就是ngInit的全部功能)。

I'm not sure of the context of your project but it sounds like ngDoCheck could be useful for what you're trying to achieve. 我不确定您的项目的上下文,但是听起来ngDoCheck对于您要实现的目标可能很有用。

ngDoCheck − This is for the detection and to act on changes that Angular can't or won't detect on its own. ngDoCheck-这是用于检测并根据Angular自身无法或不会检测到的更改采取行动。

https://www.tutorialspoint.com/angular2/angular2_lifecycle_hooks.htm https://www.tutorialspoint.com/angular2/angular2_lifecycle_hooks.htm

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM