1
當我從另一個文件試圖通話功能,我得到錯誤:AngularJS從另一個文件調用函數
在app.jsTypeError: Cannot read property 'getCurrentUserFullName' of undefined
:
'use strict';
var testApp = {};
var App = angular.module('testApp', ['testApp.filters', 'testApp.services', 'testApp.directives',
'ngRoute']);
App.run(['$location', '$rootScope', function ($location, $rootScope) {
$rootScope.$on("$routeChangeSuccess", function (event, currentRoute, previousRoute, AuthUtils) {
if(!$rootScope.userFullName){
var userFullName = AuthUtils.getCurrentUserFullName();
if(userFullName) {
$rootScope.userFullName = userFullName;
$rootScope.authenticate = true;
} else {
$rootScope.authenticate = false;
$rootScope.userFullName = "";
}
}
});
}]);
AuthUtils.js
'use strict';
angular.module('testApp')
.factory('AuthUtils', function AuthUtils($rootScope, $http) {
return {
getCurrentUserFullName: function() {
$http.get('auth/userFullName').success(function (user) {
return user;
}).error(function (data, status) {
return "error";
console.error(status, data);
});
}
};
});
爲什麼不起作用?
非常感謝你,但現在我已經一個錯誤「未捕獲的錯誤:[$注射器:unpr] HTTP://errors.angularjs。 org/1.5.0/$ injector/unpr?p0 = AuthUtilsProvider%20%3C-%20AuthUtils「 – Pablo
@Pablo請仔細檢查。我認爲我的建議在回答中應該有效.. –
謝謝,我按照上述我改變了命令導入js文件並且工作! – Pablo