如何從不同的文件(使用不同的模塊)獲取$ scope變量?例如,我有兩個文件 - index.js
和login.js
,我想從login.js
獲得用戶名index.js
。我試圖使用服務,但無法實現該目標。控制器沒有在另一個角度文件中看到服務。如何從不同的AngularJS文件獲取變量?
代碼部分給出如下:
bookApp.controller('bookListCtrl', ['sharedProperties', function($scope, $http, sharedProperties) {
'use strict';
$scope.name = "Alice";
console.log("in book controller");
console.log("getting login name: "+sharedProperties.getProperty());
和
var authentication = angular.module('authentication', []);
authentication.service('sharedProperties', function() {
var property = 'First';
return {
getProperty: function() {
return property;
},
setProperty: function(value) {
property = value;
}
};
});
我得到這個例外 -
angular.min.js:63 Error: Unknown provider: authentication.sharedPropertiesProvider <- authentication.sharedProperties at Error (native) at
在聲明其他模塊時,是否包含對模塊「身份驗證」的依賴關係? – Sarathy
你如何在你的主應用程序中包含身份驗證模塊?不要忘記你需要將它包含在你的整個模塊中。所以你的主應用程序需要包含「身份驗證」作爲一個模塊。 – PeterS
@Sarathy我包含了其他模塊,並得到了不同的例外。目前,它不能識別「身份驗證」模塊中的服務,並將其顯示爲「未定義」。 – RaufA