AngularJS中服务返回值的问题
我面临从角度服务返回数据的问题。下面是服务代码AngularJS中服务返回值的问题,angularjs,angularjs-service,Angularjs,Angularjs Service,我面临从角度服务返回数据的问题。下面是服务代码 app.service('Data',function(){ var accountList = []; var currentAccount ='Test1'; //Initialization var bookmarkAccount = function(accountName){ accountList.push(accountName); } var setPassingAccount = function(Account){
app.service('Data',function(){
var accountList = [];
var currentAccount ='Test1'; //Initialization
var bookmarkAccount = function(accountName){
accountList.push(accountName);
}
var setPassingAccount = function(Account){
console.log('in Service : setting current account value to '+Account);
currentAccount = Account;
}
return{
bookmarkAccount: bookmarkAccount,
setPassingAccount: setPassingAccount,
currentAccount: currentAccount,
accountList: accountList
};
});
我能够从“数据”服务访问除currentAccount之外的所有方法/变量。从控制器访问空值时获取空值。下面是控制器代码
app.controller('BookmarkController',function(Data){
this.accountList = [];
this.accountList = Data.accountList;
this.account = Data.currentAccount; //ISSUE
Console.log('Account list object'+accountList);
Console.log('Current account selected :'+this.account);
}
我能够获取“accountList”中的所有值,但获取空的“currentAccount”。任何人都可以在这方面提供帮助。您向我们展示的代码没有问题。在其他地方,在调用控制器之前,您可能正在使用null、undefined或作为参数调用setPassingAccount。Controller1正在使用“setPassingAccount”方法为“currentAccount”赋值。控制器2正在访问它。当从Controller2访问它时,得到的是空值……正如我所说的,问题不在您向我们展示的代码中。