Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将范围变量传递到指令';是“tAttrrs”对象吗?_Javascript_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 如何将范围变量传递到指令';是“tAttrrs”对象吗?

Javascript 如何将范围变量传递到指令';是“tAttrrs”对象吗?,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,好的,我想在动态生成的模板url中使用一个范围变量。所以我试了一下: html 我希望tAttrs.type返回$scope.type的值,但结果却是{{type}。这导致templateUrl为templates/myDirective.{{type}}.html 那么,如何获取范围变量的值而不是原始文本?范围值无法从指令的templateUrl中访问。属性尚未编译,因此在此上下文中无法访问范围 这里有一个可能适合您的解决方案 我在这里所做的是使用一个包含带有ng include的div的模

好的,我想在动态生成的模板url中使用一个范围变量。所以我试了一下:

html

我希望
tAttrs.type
返回
$scope.type
的值,但结果却是
{{type}
。这导致templateUrl为
templates/myDirective.{{type}}.html


那么,如何获取范围变量的值而不是原始文本?

范围值无法从指令的templateUrl中访问。属性尚未编译,因此在此上下文中无法访问范围

这里有一个可能适合您的解决方案


我在这里所做的是使用一个包含带有ng include的div的模板,通过双向绑定获取url。

很好,谢谢,比我尝试采用的路线简单得多。仍然不完美,但可用。
<my-directive type="{{ type }}"></my-directive>
angular.module('myApp', [])
  .directive('myDirective', function () {
    return {
      templateUrl: function (tElement, tAttrs) {
        return 'templates/myDirective.' + tAttrs.type + '.html';
      };
    };
  });