AngularJS指令通过templateUrl函数传递参数 [英] AngularJS directive pass parameters through templateUrl function

查看:381
本文介绍了AngularJS指令通过templateUrl函数传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的模板从内嵌移动到它自己的文件中。在我从模板更改为templateUrl之前,一切正常。

I'm trying to move my template from being in-line to it's own file. Everything was working before I changed from template to templateUrl

Glenn.directive('test', function($compile) {
    return {
        restrict: 'A',
        priority: 1000,
        terminal: true,
        templateUrl: function(tElement, tAttrs) {
            return ('test.html');
        },
        link: function(scope, element, attrs) {     
            attrs.$set('editable-text', 'content.' + attrs.edit + '.data');
            attrs.$set('edit', null);
            $compile(element)(scope);
        }
    }
});

test.html

{{ 'content.' + tAttrs.edit + '.data' }}

<button ng-click="' + tAttrs.edit + '_form'+ '.$show()" ng-hide="' + tAttrs.edit + '_form'+ '.$visible">edit</button>

为什么不通过 tAttrs 到我的模板test.html?

Why isn't the tAttrs being passed to my template test.html?

推荐答案

我从未见过这种方式,我总是将一个字符串传递给templateUrl属性,如

I never seen this that way, i always pass an string to the templateUrl property like

...
templateUrl: './foodirective.tmpl.html'
...

您可以在链接功能中指定指令元素中的attrs:

You could assign the attrs from your directive element in the linking function:

myApp.directive('fooDirective', function(){
  return{
    restrict: 'E',
    scope: true,
    templateUrl: './foodirective.tmpl.html',
    link: function(scope, elem, attrs){
      // do stuff
      scope.tAttrs = attrs;
    }
  }
})

我准备了一个为您插件

这篇关于AngularJS指令通过templateUrl函数传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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