我对 angularJS 服务有疑问。
我有简单的服务:
angular.module('mainApp.services', []).factory('AuthService',
function ($http) {
//var currentUser = null;
//var authorized = false;
//AutoLogin for testing
var currentUser={email: "example@example.com", id: "15"};
var authorized=true;
// initial state says we haven't logged in or out yet...
// this tells us we are in public browsing
var initialState = false;
return {
initialState:function () {
return initialState;
},
login:function (userData) {
//Call API Login
currentUser = userData;
authorized = true;
initialState = false;
},
logout:function () {
currentUser = null;
authorized = false;
},
isLoggedIn:function () {
return authorized;
},
currentUser:function () {
return currentUser;
},
authorized:function () {
return authorized;
}
};
});
然后我有一个简单的控制器
.controller('NavbarTopCtrl', ['$scope','$modal','AuthService',function($scope,$modal,authService) {
$scope.authService=authService; //IS good practice?
console.log($scope);
}])
我无法在 View 中使用我的服务。我做了简单的把戏,效果很好。
$scope.authService=authService
但是如何在我的视图(HTML 文件)中调用没有这个的服务?