https://github.com/gh0st/ncps-mms经过一些修复后对我来说很好解决...
见https://github.com/gh0st/ncps-mms/pull/2
客户端/src/routes.js
/* jshint esversion: 6 */
/* jshint node: true */
import angular from 'angular';
import 'angular-ui-router';
angular.module('ncps.routes', ['ui.router'])
.config(($stateProvider, $urlRouterProvider) => {
$urlRouterProvider.otherwise('/members/login');
$stateProvider
.state('login', {
url: '/members/login',
templateUrl: 'members/members-login.html',
controller: 'AuthController',
onEnter: ['$state', 'auth', function($state, auth) {
if (auth.isLoggedIn()) {
console.log('Going to /members/...');
$state.go('members', {
// 'headers': {
// 'Authorization': 'Bearer ' + auth.getToken()
// }
});
}
}]
})
.state('register', {
url: '/members/register',
templateUrl: 'members/members-register.html',
controller: 'AuthController',
onEnter: ['$state', 'auth', function($state, auth) {
if (auth.isLoggedIn()) {
$state.go('members');
}
}]
})
.state('members', {
url: '/members',
templateUrl: 'members/members-view.html',
resolve: {
members: function($http, auth) {
console.log('Trying to get /members....');
return $http.get('/members', {
headers: {
'Authorization': 'Bearer ' + auth.getToken()
}
}).then(function(response){
return response.data;
});
}
},
controller: 'MembersController as membersCtrl'
})
.state('new', {
url: '/members/add',
templateUrl: '/members/members-add.html',
controller: 'MembersSaveController as newMemberCtrl'
})
.state('test', {
url: '/members/test',
template: 'This is a test.'
});
});
客户端/src/controllers/controllers.js
/* jshint esversion: 6 */
/* jshint node: true */
import angular from 'angular';
angular.module('ncps.controllers', [])
.controller('MembersController', ['$http', 'auth', 'members', function($http, auth, members) {
console.log('Members retrieved');
this.members = members;
}])
.controller('MembersSaveController', function($stateParams, $state, $http) {
this.member = $state.member;
this.saveMember = function(member) {
$http.post('/members', member).then((res, member) => {
$state.go('members');
});
};
})
.controller('NavController', ['$scope', 'auth', function($scope, auth) {
$scope.isLoggedIn = auth.isLoggedIn;
$scope.currentUser = auth.currentUser;
$scope.logOut = auth.logOut;
}])
.controller('AuthController', ['$scope', '$state', 'auth', function($scope, $state, auth) {
$scope.user = {};
$scope.register = function() {
auth.register($scope.user).error(function(error) {
$scope.error = error;
}).then(function() {
$state.go('members');
});
};
$scope.logIn = function() {
auth.logIn($scope.user).error(function(error) {
$scope.error = error;
}).then(function() {
$state.go('members');
});
};
$scope.logOut = function() {
auth.logOut().error(function(error) {
$scope.error = error;
}).then(function() {
$state.go('members');
});
};
}]);