getoffer不是一个功能

时间:2015-04-13 14:20:58

标签: javascript angularjs angularjs-scope

我有工厂宣布购物中心,但我不知道为什么会收到错误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;
            });
        }
    };
});

1 个答案:

答案 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);