微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

angularjs – 如何递归要求访问父指令的控制器?

我正在递归地到达父“框”指令的控制器:
<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>

我期望控制器变量在链接函数中未定义,但是我得到了实际框指令的控制器。

所以我的问题是…如何获得对PARENT控制器的访问情况,如下所示:

<Box>
    <Box></Box>
</Box>

http://jsfiddle.net/gjv9g/1/

由于Angular 1.3,您可以使用两个口音^^在父元素“only”中搜索指令。

报价从Angular Docs on require

  • (no prefix) – Locate the required controller on the current element. Throw an error if not found.
  • ? – Attempt to locate the required controller or pass null to the link fn if not found.
  • ^ – Locate the
    required controller by searching the element and its parents. Throw an error if not found.
  • ^^ – Locate the required controller by searching the element’s parents. Throw an error if not found.
  • ?^ – Attempt to locate the required controller by searching the element and its parents or pass null to the link fn if not found.
  • ?^^ – Attempt to locate the required controller by searching the element’s parents,or pass null to the link fn if not found.

在你的情况下,替换require:’?^ Box’,需要:’?^^ Box’,

原文地址:https://www.jb51.cc/angularjs/144521.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐