Reputation: 1977
I need to initialize an object in my view and assign a reference to it.
Can I achieve this by using ng-init? Is it an assignment by value or reference?
<ANY ng-init="objA = objB"> ... </ANY>
Any help will be appreciated!
Upvotes: 2
Views: 2587
Reputation: 1061
Move assignment to controller init()
method.
ng-init
is directive that have a very lot of side effects and hardly to trace it down. For example: when you use ng-init
in directive template for creating/editing item and you assign some model
value in it - you will achieve problem with editing that actually should use already existing value.
As well side effect of it is executing few times when you add ng-if
.
Usage ng-init
in templates are your own risk.
Right way: controller
data should be defined at start of module - in any case any view started in order: $state
-> resolve()
-> controller
-> template
-> directive
. It's not a good idea to fool yourself with not existing data until it will be created by magic.
In case of repeaters when you have for example empty {}
and you need to display it like possibility to fill yet empty input, as I mentioned - you need to run function on init that can define empty or extend existing model by passing actual model.
Upvotes: 1