我一直在阅读很多文档,而且我越来越困惑了.我基本上无法弄清楚a之间的区别
我看到很多自定义组件.有时候他们会使用指令,有时候是服务.它始终以模块开始.有人可以用一个例子来解释这三种类型之间的区别吗?
我试图以递归方式到达父"box"指令的控制器:
<body ng-app="main">
<!-- no nesting: parent is the just body -->
<box></box>
<script type="text/javascript">
angular.module('main', [])
.directive('box', function() {
return {
restrict: 'E',
controller: function() { },
require: '?^box', // find optional PARENT "box" directive
link: function(scope, iElement, iAttrs, controller) {
// controller should be undefined, as there is no parent box
alert('Controller found: ' + (controller !== undefined));
}
};
});
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
我希望控制器变量undefined在链接函数中,但我得到实际box指令的控制器.
所以我的问题是......在这种情况下如何访问PARENT控制器:
<box>
<box></box>
</box>
Run Code Online (Sandbox Code Playgroud)
我有一种情况,我必须创建一个带有一些花里胡哨的容器指令..然后我有一个可以包装到该容器指令中的指令列表.
像这样的东西:
<the-container foo="bell" bar="whistle">
<some-directive></some-directive>
</the-container>
<the-container foo="potato" bar="tomato">
<some-other-directive></some-other-directive>
</the-container>
Run Code Online (Sandbox Code Playgroud)
有没有什么办法中,我可以<some-directive>和<some-other-directive>认识的foo和/或bar属性,它们在transcluded指令的价值观?
基本的theContainer指令
.directive("theContainer", function() {
return {
restrict: "E",
replace: true,
transclude: true,
scope: true,
templateUrl: "theContainer.html",
compile: function(element, attrs) {
return function(scope, element, attrs) {
scope.containerAttrs = {
foo: attrs.foo,
bar: attrs.bar
};
};
}
}
});
Run Code Online (Sandbox Code Playgroud)
让我们假设这两个指令一起具有不同且无关的功能
someDirective
.directive("someDirective", function() {
return {
restrict: "E",
scope: true,
templateUrl: "someDirective.html",
controller: function($scope, $element, $attrs) {},
compile: function(element, attrs) { …Run Code Online (Sandbox Code Playgroud)