I have a function checkLogin()
which sets a variable in a service. This is always run in app.run(...)
. In one function called by ng-init I want to use this variable to make a call to my backend. The problem is that ng-init is called before, so the call to my backend sends an undefined
variable. What should I do to make sure that checkLogin()
is always called first?
Asked
Active
Viewed 1,096 times
0

Myone
- 1,103
- 2
- 11
- 24
-
you could use `resolve` property of `state`/`route`, in that you could have `checkLogin()` method check.. so resolve will make that call before loading `templateUrl` with `controller`. you could refer [this answer](http://stackoverflow.com/a/22540482/2435473) for more information – Pankaj Parkar Apr 22 '16 at 18:02
-
You should use promises and callbacks to achieve what you want instead of `ng-init`. https://docs.angularjs.org/api/ng/service/$q – David Meza Apr 22 '16 at 18:13
1 Answers
0
Like this Here login required is a function of service
.state('app.dashboard', {
url: "/dashboard",
templateUrl: "partials/dashboard.html",
controller: 'DashboardCtrl',
activePage:'dashboard',
resolve: {
loginRequired: loginRequired
}

Wasiq Muhammad
- 3,080
- 3
- 16
- 29
-
This worked, thanks. However, one thing doesn't work. In my index.html page I include a partial navbar.html and inject a controller homeController. However, how do I use resolve here, since it's not a route? – Myone Apr 23 '16 at 08:10