Can I inject a service into a directive in AngularJS?

后端 未结 3 1360
深忆病人
深忆病人 2020-11-27 09:44

I am trying to inject a service into a directive like below:

 var app = angular.module(\'app\',[]);
 app.factory(\'myData\', function(){
     return {
             


        
3条回答
  •  温柔的废话
    2020-11-27 10:25

    Change your directive definition from app.module to app.directive. Apart from that everything looks fine. Btw, very rarely do you have to inject a service into a directive. If you are injecting a service ( which usually is a data source or model ) into your directive ( which is kind of part of a view ), you are creating a direct coupling between your view and model. You need to separate them out by wiring them together using a controller.

    It does work fine. I am not sure what you are doing which is wrong. Here is a plunk of it working.

    http://plnkr.co/edit/M8omDEjvPvBtrBHM84Am

提交回复
热议问题