Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.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 动态模板和范围变量_Javascript_Angularjs_Angularjs Directive_Angularjs Scope - Fatal编程技术网

Javascript 动态模板和范围变量

Javascript 动态模板和范围变量,javascript,angularjs,angularjs-directive,angularjs-scope,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,我在理解指令中传递的指令属性与范围变量时遇到一些困难。 我正在编写一个指令,根据内容类型动态加载正确的模板。 当我使用链接器函数使用范围变量进行切换时,一切正常 如果我使用: <mydirective ng-repeat="item in items" content="item" type="item.type"></mydirective> 现在,我想向“templateUrl”传递一个函数,如下所示: return { restrict : "E",

我在理解指令中传递的指令属性与范围变量时遇到一些困难。
我正在编写一个指令,根据内容类型动态加载正确的模板。 当我使用
链接器
函数使用范围变量进行切换时,一切正常

如果我使用:

<mydirective ng-repeat="item in items" content="item" type="item.type"></mydirective>
现在,我想向“templateUrl”传递一个函数,如下所示:

return {
    restrict : "E",
    replace: true,
    scope: {
        'content': "="
    },
    templateUrl : function(element, attributes) {
      // I don't access to the scope but only to the attributes
      attributes.$observe(attributes.content.type, function(value){
        if(value) {
          // templates is a service 
          return templates.getTemplateUrl(value.type)
        }
      })
    }
    //[...]
}
现在,我观察
attributes.content.type
attributes.type
,这个都不起作用。这些属性总是
未定义的

我还尝试将
类型
添加为传递给指令的范围变量:

scope: {
         'content': "="
         'type': '='
        }
但它一直是
未定义的

所以基本上我对
属性的使用和
变量范围的使用感到困惑

编辑:

我想这与ng repeat有关。如果我在线路上设置断点

attributes.$observe(attributes.content.type, function(value){
我检查得到的
属性

$$element: jQuery.fn.init[1]
$attr: Object
content: "item"
ngRepeat: ""item in items"

所以我猜
content
尚未计算,这就是
attributes.content.type
未定义的原因。想知道为什么..

您的
模板是同步的还是异步的?函数必须返回一个表示Url的字符串,并且不支持返回承诺(我必须在Plunker中测试它才能确定)

如果它确实同步返回字符串,那么我不确定为什么
attributes.type===undefined

类似于以下工作:

templateUrl : function(element, attributes) {
   return attributes.type;
}
我用你的代码(稍微修改过的版本)试过了,效果很好——下面是答案

至于您的问题,当您实现单向(即指令对值的更改作出反应)或双向绑定(即指令作出反应并可以更改父范围中的值)时,请使用
scope
。使用属性作为初始化值

编辑: 我现在更了解这个问题了。这里有几件事:

1应插入指定给类型属性的值(即在大括号内):


这是完整的。

我的
模板
服务是同步的。我已经更新了这个问题,我想这与ng repeat更相关,但不是100%确定..所以基本上你的上一个解决方案看起来与我提出的第一个解决方案(唯一有效的解决方案)非常相似,不是吗?只是在这个意义上,它在链接文件中,而不是templateUrl函数中。但现在,您并不真正需要模板html检索服务。
templateUrl : function(element, attributes) {
   return attributes.type;
}
<mydirective ng-repeat="item in items" content="item.content" type="{{item.type}}"></mydirective>
link: function(scope, elem, attr){
    var template = attr.type || "template1";
    var templateUrl = template + ".html";
    elem.append("<div ng-include='\"" + templateUrl + "\"'></ng-include>");

    $compile(elem.contents())(scope);
},