Ng-int gets executed thrice


#1

I m initializing my template using ng-init. On ng-init, I m calling a function XXX() inside a controller. XXX() gets called more than once. XXX() requests for data from service which is also requested more than once. As a result of this, I m displaying redundant data in my template.
I have not assigned the controller in the template using ng-controller. I have assigned the controller to the template only in the Route definition like this
.state(‘xxxx’, {
url: ‘/xxxx’,
views: {
‘xxxx’: {
templateUrl: ‘templates/xxxx-page.html’,
controller: ‘XXXXXCtrl’
}
}
})

How to combat this issue @leob ? I just need to request the service only once…


#2

everytime your dom element gets removed from the dom and readded ng-init gets called…

like on repeats --> if the array changes or ng-if when the condition gets falsy


#3

Why do you use ng-init? I’ve heard about it but I heard that its use is not encouraged, I’ve never used it.

If I want to initialize stuff in my controller then I just use $scope.$on(’$ionicView.beforeEnter’… (execute whenever the view is enetered).

Or simply have it executed in my controller function body (but then it will get executed only once really during the controller’s lifetime).