Edu*_*nal 1 javascript angularjs angularjs-directive
特定
<mytag class="red">
<mysubtag id="3"></mysubtag>
</mysubtag>
Run Code Online (Sandbox Code Playgroud)
子标签可能具有不同的含义,具体取决于父标签(可能是mytag或mysupercustomtag).我有所有父标签的指令.我如何访问子标签?
我知道在mytag我可以使用该指令的链接功能children(),contents()以及data(),甚至elm.find('#someid').像下面这样的东西可以做到吗?这样做的正确方法是什么?嵌套指令?
.directive('mytag', function() {
return {
restrict: 'A',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
// get the id of the first child (for example mysubtag)
var newid = element.children()[0].getAttribute('id');
// and put it in mytag, so that it is transformed into <div class="red" id="3">..
attrs.$set('id', newid);
}
}
})
Run Code Online (Sandbox Code Playgroud)
这种方法不正确.对于嵌套指令,您必须在父指令中声明控制器,并在子指令中使用require属性来访问父控制器.请参阅以下有关子父指令的角度文档中的示例.以下链接将帮助您设置一个
http://angularjs.org/#create-components
| 归档时间: |
|
| 查看次数: |
5156 次 |
| 最近记录: |