加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS – 将工厂注入指令的链接功能

发布时间:2020-12-17 08:05:55 所属栏目:安全 来源:网络整理
导读:我有一个简单的代码: define(['app'],function(app){ app.factory('factoryProvider',function(){ return { name: 'my Name' } }); app.directive('myDiv',['factoryProvider',function(factoryProvider) { return { restrict: 'E',replace: true,templateU
我有一个简单的代码:
define(['app'],function(app)
{
    app.factory('factoryProvider',function(){
        return {
            name: 'my Name'
        }
    });

    app.directive('myDiv',['factoryProvider',function(factoryProvider) {
        return {
            restrict: 'E',replace: true,templateUrl: 'link/to/template.html',controller: function($scope) {
            },link: function(scope,routeParams,location) {
                console.log(factoryProvider.name);
            }
        };   
    }])
});

我想要能够在链接功能中访问myFactory,但是我不能!我也尝试过link:function(scope,routeParams,location,factoryProvider),也没有工作。为什么?

它应该已经在链接功能内部可用了
app.factory('factoryProvider',function(){
    return {
        name: 'my Name'
    }
});

app.directive('myDiv',function(factoryProvider) {
    return {
        restrict: 'E',template: '<p>{{name}}</p>',controller: function($scope) {
        },link: function(scope) {
            scope.name=factoryProvider.name;
        }
    };
}]);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读