AngularJS:工厂中的调用承诺方法
我有一个如下方式的角度工厂,但当我尝试调用getDepositAccountDetailsService时,我得到以下错误:AngularJS:工厂中的调用承诺方法,angularjs,angularjs-service,angularjs-factory,Angularjs,Angularjs Service,Angularjs Factory,我有一个如下方式的角度工厂,但当我尝试调用getDepositAccountDetailsService时,我得到以下错误: TypeError: this.getDepositAccountDetails.getDepositAccountDetailsService is not a function 如何称呼工厂内部的承诺 tellApp.factory('TabsFactory', function($resource){ var activetabs = {};
TypeError: this.getDepositAccountDetails.getDepositAccountDetailsService is not a function
如何称呼工厂内部的承诺
tellApp.factory('TabsFactory', function($resource){
var activetabs = {};
activetabs.getDepositAccountDetails = function(){
$resource('XXXXXXX/:number', {}, {
getDepositAccountDetailsService: { method: 'GET', isArray: false}
});
}
activetabs.setAccountInfo = function(accountnumber, result) {
var accountinit = {
accountInfo:[]
};
if(result.code == "s") {
this.getDepositAccountDetails.getDepositAccountDetailsService({number : accountnumber}).$promise.then(function(response){
return accountinit.accountInfo = response;
}, function(error) {
});
}
}
return activetabs;
});
控制器
我想你在代码中遗漏了一些东西
getDepositAccountDetails
this.getDepositAccountDetails
应该是activetabs.getDepositAccountDetails()
,因为您为工厂上下文创建了一个变量tellApp.factory('TabsFactory', function($resource) {
var activetabs = {};
activetabs.getDepositAccountDetails = function() {
return $resource('XXXXXXX/:number', {}, {
getDepositAccountDetailsService: {
method: 'GET',
isArray: false
}
});
}
activetabs.setAccountInfo = function(accountnumber, result) {
var accountinit = {
accountInfo: []
};
if (result.code == "s") {
activetabs.getDepositAccountDetails().getDepositAccountDetailsService({
number: accountnumber
}).$promise.then(function(response) {
return accountinit.accountInfo = response;
}, function(error) {
});
}
}
return activetabs;
});
activetabs.getDepositAccountDetails
应该返回$resource
对象,而不是这个.getDepositAccountDetails
你应该执行activetabs.getDepositAccountDetails
我已经返回了$resource,但是仍然是同样的问题,你是否遵循了我提到的第二件事……或者现在你添加了var-activetabs=this代码>而不是var-activetabs={}代码>抱歉,pankaj..我仍然收到相同的问题Peerror:activetabs.getDepositAccountDetails.getDepositAccountDetails服务不是function@vishnu很乐意帮忙,谢谢:)
tellApp.factory('TabsFactory', function($resource) {
var activetabs = {};
activetabs.getDepositAccountDetails = function() {
return $resource('XXXXXXX/:number', {}, {
getDepositAccountDetailsService: {
method: 'GET',
isArray: false
}
});
}
activetabs.setAccountInfo = function(accountnumber, result) {
var accountinit = {
accountInfo: []
};
if (result.code == "s") {
activetabs.getDepositAccountDetails().getDepositAccountDetailsService({
number: accountnumber
}).$promise.then(function(response) {
return accountinit.accountInfo = response;
}, function(error) {
});
}
}
return activetabs;
});