将服务注入角度1.5组件

时间:2017-12-06 09:15:32

标签: javascript angularjs service components

我试图从angular1.5组件初始化test.service.js而我真的不知道如何做到这一点。我已将此结构用于角度分量:

var testComponent =  {
    templateUrl: "app/components/test.component.html",
    controllerAs: "mvm",
    bindings:{
        bindingInput: "=",
},
controller: function(){
      var mvm = this;

    }
};
angular.module('myApp').component('testComponent', testComponent);

2 个答案:

答案 0 :(得分:1)

您可以将服务权限注入控制器,如下所示:

....
    controller: function(testService) {
      var mvm = this;
    }
};

为防止{min}插件对testService进行模糊处理,请让注入服务器了解相关性。

var Controller = function(testService) {
   // Your controller code here
};

Controller.$inject = ['testService'];

var testComponent = {
  ...
  controller: Controller, // Assign controller in component
  ...
};

答案 1 :(得分:1)

使用控制器注入它,如下所示:

...
},
controller: ['testService', function(testService){
  var mvm = this;
}],
...