相关疑难解决方法(0)

在Transcluded Directive中访问父范围

我想访问父指令的范围,但我似乎无法获得正确的设置组合.这是可能的,这是正确的方法吗?

我真的想避免在MyCtrl中放置类似SOME_CONST(这可以帮助我通过控制流程进行DOM更新)

<div ng-controller="MyCtrl">
    <parent>
        <child></child>
    </parent>
</div>

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.obj = {prop:'foo'};
}

myApp.directive('parent', function() {
    return {
        scope: true,
        transclude: true,
        restrict: 'EA',
        template: '<div ng-transclude><h1>I\'m parent {{obj.prop}}<h1></div>',
        link: function(scope, elem, attrs) {
            scope.SOME_CONST = 'someConst';
        }
    }
});

myApp.directive('child', function() {
    return {
        restrict: 'EA',
        template: '<h1>I\'m child.... I want to access my parent\'s stuff, but I can\'t.  I can access MyCtrlScope though, see <b>{{obj.prop}}</b></h1> how can I access the <b>SOME_CONST</b> value …
Run Code Online (Sandbox Code Playgroud)

angularjs angularjs-directive angularjs-scope

29
推荐指数
3
解决办法
3万
查看次数