I would say, that you are almost there.
Just the service/factory must return something. I mean here:
...
message: function(testservice) {
return testservice.getdata();
}
we expect something... and there was nothing
I added one line return data;
and there is that updated plunker
.factory('testservice', ['$http',
function testservice($http) {
// interface
// implementation
function getData() {
return $http.get("http://jsonplaceholder.typicode.com/photos")
.then(function(data) {
console.log(data)
// HERE - this line was missing
// return something here
return data;
}, function(error) {
console.log(error)
})
EXTEND: 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
<div >
<div ui-view="">
<h2>...loading...</h2>
</div>
</div>
which is a view of a brand new parent state 'loadingB'
:
.state('loadingB', {
redirectTo: "b",
templateUrl : "loadingB.html",
})
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:
.run(['$rootScope', '$state', function($rootScope, $state) {
$rootScope.$on('$stateChangeSuccess', function(evt, to, params) {
if (to.redirectTo) {
evt.preventDefault();
$state.go(to.redirectTo, params)
}
});
}])
And our service now uses $timeout
to get some delay:
.factory('testservice', ['$http', '$timeout',
function testservice($http, $timeout) {
// interface
// implementation
function getData() {
return $http.get("http://jsonplaceholder.typicode.com/photos")
.then(function(data) {
console.log("resolved http")
return $timeout(function(){
console.log("after two seconds delay")
return data;
}, 2500)
...
And finally, we have to redirect to loadingB
here
$scope.moveto = function() {
$state.go('loadingB');
}
And also make the 'b' child of 'laodingB'
.state("b", {
parent: "loadingB",
templateUrl: "b.html",
url: "/b",
controller: 'b',
resolve: {
message: function(testservice) {
return testservice.getdata();
},
Check it all here