var app = angular.module('demo', ['ionic'])
.config(['$stateProvider', function ($stateProvider) {
$stateProvider
.state("user", {
url : "/user",
templateUrl: 'user.html',
controller : 'userCtrl',
resolve: {
// resolve basic info
userSam: function(){
return {name: 'Sam', job:'Boss', id: Math.random()}
},
userJane: function(userService){
return userService.getJane();
},
// resolve a promise
userJoe: function(userService){
return userService.getJoeQ(true);
},
// get a promise then make a change.
userAlias: function(userService){
return userService.getJoeQ(true).then(function(result){
result.name = 'Joseph';
return result;
},function(err){
throw err;
});
}
}
})
.state("baduser", {
url : "/baduser",
templateUrl: 'user.html',
controller : 'userCtrl',
resolve: {
// resolve basic info
userSam: function(){
return {name: 'Sam', job:'Boss', id: Math.random()}
},
userJane: function(userService){
return userService.getJane();
},
// resolve a promise
userJoe: function(userService){
// This should fail
return userService.getJoeQ(true);
},
// get a promise then make a change.
userAlias: function(userService, $state){
return userService.getJoeQ(false).then(function(result){
result.name = 'Joseph';
return result;
},function(err){
console.error(err);
$state.go('user',{reload: true});
});
}
}
})
}]);
app.controller('userCtrl', function($scope, $state, userSam, userJane, userJoe, userAlias ) {
// check if we can see them
if(userSam){
console.log('Sam: '+JSON.stringify(userSam));
}
if(userJoe){
console.log('Joe: '+JSON.stringify(userJoe));
}
if(userAlias){
console.log('Alias: '+JSON.stringify(userAlias));
}
// assign them to our scope
$scope.sam = userSam;
$scope.jane = userJane;
$scope.joe = userJoe;
$scope.alias = userAlias;
$scope.reload = function(){
$state.reload();
};
});
app.factory('userService', function($q,$timeout,$state) {
var service = {};
service.getJane = function(){
return {name: 'Jane', job: 'Programmer', id: Math.random()}
};
service.getJoeQ = function(maybe){
return $q(function(resolve, reject) {
// set a timeout to waste some time
$timeout(function(){
if(maybe){
resolve({name: 'Joe', job: 'Programmer', id: Math.random()});
} else {
$timeout(function(){
$state.go('user',{});
reject('Request Failed!');
});
}
}, 2000);
});
};
service.reload = function(destination){
return $q(function(resolve, reject) {
console.log("Reloading.");
// set a timeout to waste some time
$timeout(function(){
$state.go(destination,{});
reject("Reload!");
});
});
};
return service;
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="ionic@1.3.1" data-semver="1.3.1" rel="stylesheet" href="https://code.ionicframework.com/1.3.1/css/ionic.min.css" />
<script data-require="ionic@1.3.1" data-semver="1.3.1" src="https://code.ionicframework.com/1.3.1/js/ionic.bundle.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="app.js"></script>
</head>
<body ng-app="demo">
<nav class="navbar navbar-inverse" role="navigation">
<div class="navbar-header">
<a class="navbar-brand" ui-sref="#">AngularUI Router</a>
</div>
<ul class="nav navbar-nav">
<li>
<button ui-sref="user">User</button>
</li>
<li>
<button ui-sref="baduser">Broken User</button>
</li>
</ul>
</nav>
<!-- MAIN CONTENT -->
<!-- THIS IS WHERE WE WILL INJECT OUR CONTENT -->
<div class="container">
<div ui-view=""></div>
</div>
</body>
</html>
/* Put your css in here */
<button ng-click="reload()">Reload</button>
<div>
<p>Hello {{sam}}!</p>
<p>Hello {{jane}}!</p>
<p>Hello {{joe}}!</p>
<p>Hello {{alias}}!</p>
</div>