Javascript 角度嵌套指令仅渲染第一个子对象
我有一个Javascript 角度嵌套指令仅渲染第一个子对象,javascript,angularjs,Javascript,Angularjs,我有一个parent指令,它包括两个子指令,first和second。我注意到只有第一个孩子被渲染。另外,如果我在第一个HTML标记之前放置了一些任意的HTML标记,那么它们都会被呈现出来,但是如果我将它们放在第一个HTML标记之后,它们就不会出现。为什么会这样 见: var-app=angular.module(“myApp”,[]); app.directive(“myParentDir”,function()){ 返回{ 限制:'E', 模板:“” }; }); 指令(“myFirstC
parent
指令,它包括两个子指令,first
和second
。我注意到只有第一个孩子被渲染。另外,如果我在第一个HTML标记之前放置了一些任意的HTML标记,那么它们都会被呈现出来,但是如果我将它们放在第一个HTML标记之后,它们就不会出现。为什么会这样
见:
var-app=angular.module(“myApp”,[]);
app.directive(“myParentDir”,function()){
返回{
限制:'E',
模板:“”
};
});
指令(“myFirstChild”,函数(){
返回{
限制:'E',
模板:“”,
};
});
app.directive(“mySecondChild”,function()){
返回{
限制:'E',
模板:“”,
};
});
试着像这样使用它:
var app = angular.module("myApp", []);
app.directive("myParentDir", function() {
return {
restrict: 'E',
template: '<my-first-child></my-first-child> <my-second-child></my-second-child>'
};
});
var-app=angular.module(“myApp”,[]);
app.directive(“myParentDir”,function()){
返回{
限制:'E',
模板:“”
};
});
从角度来看:
html规范定义的自动关闭或无效元素非常重要
专用于浏览器解析器。你不能自己做,所以为了你自己
自定义图元必须粘贴到非空心图元()
这在角度上是无法改变的
自定义标记不是叶标记,因此您必须使用:
template:'<my-first-child></my-first-child> <my-second-child></my-second-child>'
模板:“”
因为您使用的是自定义标记,所以需要关闭标记,因为HTML规范不允许自动关闭标记
template: '<my-first-child></my-first-child> <my-second-child></my-second-child>'
模板:“”
谢谢。比我想象的要容易。这是最新的。
template: '<my-first-child></my-first-child> <my-second-child></my-second-child>'