I found that ng-init is not updated after rowIndex is updated.
<div ng-init="item = getItem(rowIndex)">
{{ item.Name }}
</div>
item is not updated after rowIndex is changed (even if changes are made with $timeout)
Is there other directive that allows to avoid using the following dirty trick:
<div ng-repeat="item in [getItem(rowIndex)]">
{{ item.Name }}
</div>
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.
In this case where item has more that one property, the getItem function needs to be invoked only once.
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.
ng-initAvoid using ng-init. It tangles the Model and View, making code difficult to understand, test, debug, and maintain. Instead initialize the Model in the controller.
From the Docs:
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 ofngRepeat, as seen in the demo below; and for injecting data via server side scripting. Besides these few cases, you should use controllers rather thanngInitto initialize values on a scope.— AngularJS ng-init Directive API Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With