javascript - AngularJS 指令通过 templateUrl 函数传递参数

标签 javascript angularjs angularjs-directive

我正在尝试将我的模板从内联移动到它自己的文件中。在我从模板更改为 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?

最佳答案

我从来没有见过这种情况,我总是将一个字符串传递给 templateUrl 属性,例如

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

您可以在链接函数中分配指令元素中的属性:

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

我准备了一个 plunk给你。

关于javascript - AngularJS 指令通过 templateUrl 函数传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918038/

相关文章:

angularjs - 在 Angular Material 中添加意图组

javascript - 使用带 Angular 外部库

javascript - 文档点击事件在过滤器上停止

javascript - 在 DrawerNavigator 中隐藏标题( react 导航)

php - 在文件输入更改时上传文件

javascript - angular-translate:如何加载静态 json 文件?

php - JSON 返回数组的奇怪结果

jquery - 如何使用 Angularjs 包含页眉和页脚?

javascript - 同一指令的 Angular 多个实例,范围不是孤立的

javascript - 用于复选框上 ng-indeterminate 属性的 AngularJS 自定义指令