This is the expected behavior. If scope.init()
is being called on init, it should be called on page load, as the scope will be bound (and initialized) each time its controller route will be accessed.
To avoid that behavior, simply call init()
on demand, or — better yet — escalate postList
to a higher level in the scope hierarchy (above ng-view
, where the route change takes place and re-binds scopes to views), e.g. in the $rootScope
. That way its initialization / evaluation won't be tied to the $s
scope's init.
To illustrate this [1]:
You can define
postList
in the topmost scope, — it will be prototypically (is that even a word?) inherited:$rootScope.postList = [];
It's also sufficient to save it in a parent controller — so long as it's higher in the hierarchy than the router's scope (where
ng-view
resides), as same rules apply for inheritance with controller's scopes. Something along these lines [2]:// in the view %div { ng_contoller: 'ParentCtrl' } %div { ng_view } %div{ ng_controller: 'PostCtrl', ng_init: 'init()', ng_cloak: true }
// in ParentCtrl $s.postList = [];
[1] Caution! not tested!
[2] Risk of bogus code — I don't really know HAML!