【问题标题】:AngularJS directive templateUrl function using 2-way data binding [duplicate]AngularJS指令templateUrl函数使用2路数据绑定[重复]
【发布时间】:2016-12-14 02:11:10
【问题描述】:

我尝试使用指令参数化页面模板。 我有一个具有“类型”值的对象数组。当类型不同时,我想使用不同的模板。

这是我尝试过的:

directive.js

angular.module('core')
  .directive('mySolutionDisplay', function () {
    return {
      restrict: 'E',
      scope: {
        solution: '='
      },
      templateUrl: function (elem, attr) {
        return 'path/to/template/solution-'+attr.type+'.template.html';
      }
    };
  });

view.html

<div class="row">
  <my-solution-display type="vm.solution[0].type" solution="vm.solution"></my-solution-display>
</div>

我收到以下错误: angular.js:11706 Error: [$compile:tpload] Failed to load template: path/to/template/solution-vm.solution[0].type.template.html

我尝试将type="vm.solution[0].type" 替换为type="{{vm.solution[0].type}}",但它只是在错误消息中添加了大括号。

【问题讨论】:

  • 解决方案显示>
  • 你试过这样吗,我认为在 type="" 它不能解析变量名,你可以尝试在那里添加花括号

标签: javascript angularjs angularjs-directive


【解决方案1】:

属性(attrs)无法访问范围变量。它们被插值后得到 dom 值。

所以,使用

<my-solution-display type="{{vm.solution[0].type}}" solution="vm.solution"> </my-solution-display>

注意type="vm.solution[0].type" 是如何更改为type="{{vm.solution[0].type}}"

【讨论】:

  • 我已经尝试过了,但它只是在错误描述中添加了大括号。 angular.js:11706 Error: [$compile:tpload] Failed to load template: path/to/template/solution-{{vm.solution[0].type}}.template.html
  • 是的,忽略我的回答。看起来那是不可能的。查看错误:github.com/angular/angular.js/issues/13526。看看:stackoverflow.com/questions/21835471/… 了解解决此问题的另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-05
  • 1970-01-01
  • 2018-06-13
相关资源
最近更新 更多