我想知道这样的方法是否
(这是一个非常微不足道的例子)
可能是管理的起点
angularjs + 用户认证
<!DOCTYPE html>
<html>
<head>
<title>My auth test</title>
</head>
<body>
<div data-ng-app="myApp">
<div data-ng-view></div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.factory('Devs', function() {
var data = [{name:'Joe',auth:false},{name:'Whisher',auth:true}];
return data;
});
app.factory('Projects', function() {
var data = [{name:'Php'},{name:'Javascript'}];
return data;
});
app.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'OneCtrl',
resolve: {
projects: function(Projects) {
return Projects;
}
},
templateUrl:'./view.html'
}).
when('/one', {
controller: 'OneCtrl',
resolve: {
projects: function(Projects) {
return Projects;
}
},
templateUrl:'./view.html'
}).
when('/two', {
controller: 'TwoCtrl',
resolve: {
projects: function(Projects) {
return Projects;
}
},
templateUrl:'./view.html'
})
.otherwise({redirectTo:'/'});
});
app.controller('OneCtrl',function($scope,Devs, projects) {
$scope.project = projects[0];
$scope.dev = Devs[0];
});
app.controller('TwoCtrl',function($scope,Devs, projects) {
$scope.project = projects[1];
$scope.dev = Devs[1];
});
app.directive('checkAuth',function($location){
return {
link:function(scope){
scope.$on('$routeChangeSuccess', function(next, current) {
if(!scope.dev.auth){
$location.path('/');
}
});
}
}
});
</script>
</body>
</html>
在视图中
<div check-auth>
<p>{{project.name}}</p>
<div><a ng-href="./#/one">one</a></div>
<div><a ng-href="./#/two">two</a></div>
<div>
您只能在需要身份验证的视图中使用该指令。
你有什么想法 ?我对一些链接也很满意:)