注射服务指令 [英] Injecting service to Directive

查看:108
本文介绍了注射服务指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注入一个服务指令像下面

I am trying to inject a service to Directive like below

 var app = angular.module('app',[]);
 app.factory('myData', function(){
     return {
        name : "myName"
     }
 });
 app.directive('changeIt',function($compile, myData){
    return {
            restrict: 'C',
            link: function (scope, element, attrs) {
                scope.name = myData.name;
            }
        }
 });

但是,这是我返回错误未知提供商:myDataProvider 。有人请看code和告诉我,如果我做错了什么地方。

But this is returning me error Unknown provider: myDataProvider. Someone please look into the code and tell me if I am doing wrong somewhere..

推荐答案

您可以做注射指令,它看起来就像它在其他地方。

You can do injection on Directives, and it looks just like it does everywhere else.

app.directive('changeIt', ['$compile', 'myData', function($compile, myData){
    return {
        restrict: 'C',
        link: function (scope, element, attrs) {
            scope.name = myData.name;
        }
    }
 }]);

这篇关于注射服务指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆