使用ngInclude时继承父函数

时间:2015-03-09 09:30:16

标签: angularjs angularjs-directive angularjs-scope

我有一个使用ngInclude的指令,我想调用parent ctrl函数。这就是我现在的做法,但它不起作用。

//MyCtrl
$scope.shareLink = function () {
    // do something
};
<div ng-controller="MyCtrl as ctrl"> 
    <my-directive></my-directive>  
    <script type="text/ng-template" id="share-link-actions.html">
        <a href="#" ng-click="$parent.$parent.shareLink();">Share</a>
    </script>
</div>

<!-- myDirective --> 
<div class="myDirective1">
    <div ng-include="share-link-actions.html"></div>
</div>

1 个答案:

答案 0 :(得分:0)

试试这样:

JS

directive("myDirective", function() {
    return {
        restrict: "E",
        template: "<a href='#' ng-click='shareLink();'>Share</a>"
    };
});

标记

<div ng-controller="MyCtrl as ctrl"> 
    <my-directive></my-directive>  
</div>