我有工厂宣布购物中心,但我不知道为什么会收到错误get offer is not a function!
.controller('confirmCtrl', function($scope,Malls,$stateParams,$log) {
$scope.offers = Malls.getoffer($stateParams.offersId);
console.log($scope.offers,"test");
})
var offers=[{
id:0,
message:'Are you sure want to buy this coupon?'
}, {
id:1,
message:'Are you sure want to buy this coupon?'
}];
return {
all:function(){
return offers;
},
getoffer: function(offersId) {
return _.find(offers, function(offers) {
return offers.id == offersId;
});
}
};
});
答案 0 :(得分:1)
控制器构造函数中Malls
的值由angular的依赖注入处理。因此,必须告知角度Malls
是否能够将其注入控制器。
根据您在上面定义的代码,Malls
只是一个全局函数,根据该假设,您可以执行以下操作:
<Your Module>.service('Malls', Malls);
第一个参数'Malls'
需要与控制器构造函数中的参数匹配。
第二个参数是服务的函数/构造函数。
<强>更新强>
// declare your service...
function Malls(){
var offers = [{
id:0,
message:'Are you sure want to buy this coupon?'
}, {
id:1,
message:'Are you sure want to buy this coupon?'
}];
this.all = function(){
return offers;
};
this.getOffer = function(id){
var offer;
// logic to determine offer
return offer;
};
}
// add your service to your angular module...
<Your Module>.service('Malls', Malls);