How to remove undefined error in angular js ?Actually i am trying to load data in using resolve and use that data in controller but on controller i am getting undefined why ?
resolve: {
message: function (testservice) {
return testservice.getdata();
},
message2: function (testservice) {
return testservice.getTodo();
},
message3: function (testservice) {
return testservice.getGithub();
}
}
use the controller
.controller('b', function($scope, $stateParams, $state, $ionicHistory,message) {
console.log("controller is loaded"+message)
})
// undefined in this console
console.log("controller is loaded"+message)
I would say, that you are almost there.
Just the service/factory must return something. I mean here:
we expect something... and there was nothing
I added one line
return data;
and there is that updated plunkerEXTEND: How to show some view ...loading... before resolve is done?
Based on some comments I extended the example here. It is now showing this view:
loadingB.html
which is a view of a brand new parent state
'loadingB'
:It injects the above view loadingB.html in position of original state 'b'. And it has one property
redirectTo: "b"
, which is managed by this small piece of code:And our service now uses
$timeout
to get some delay:And finally, we have to redirect to
loadingB
hereAnd also make the 'b' child of 'laodingB'
Check it all here
Radim Koehler has the correct answer, or even easier, just return the promise from the getData function in the service:
If you want to resolve state before going into view, then you need to do few things. resolve you state like this
in you services you can have factory method like :
I have created similar plunker demo here