评估指令内的变量

问题描述 投票:0回答:1

有没有办法在指令中评估src?这是我的片段:

app.directive('loadTemplate', function ($templateCache,$compile) {
      return {
          restrict: 'A',
          scope: {
              src: '@'
          },
          template:$templateCache.get("'{{src}}'")
      };
  });
angularjs angularjs-directive
1个回答
0
投票

使用templateUrl它可以接受一个函数:

app.directive('loadTemplate', function ($templateCache,$compile) {
    return {
        restrict: 'A',
        scope: {
            src: '@'
        },
        templateUrl: function (elem, attrs) {
            return attrs.src;
        }
    };
});

在你的HTML中:

<loadTemplate src="xxx.html"></loadTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.