0

私が間違っている場合は修正してください。ただし、シナリオでは、兄弟スコープを作成しているためfirstDirective、の動作を達成できないと思います。secondDirectiveテンプレートのコントローラーのスコープにアクセスできません。secondDirective透視の力での振る舞いが欲しい。これを達成する方法はありますか?または、この問題を間違った方法で攻撃していますか?

http://jsfiddle.net/3QRDt/68/

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

app.directive('firstDirective', function(){
    return {
        restrict: 'EA',
        replace: true,
        scope: true,
        transclude: true,
        template: '<div id="holder" data-ng-controller="MyController">{{shouldBeOpen}}<div ng-transclude></div><button data-ng-click="close()">Close</button></div>',
        link: function(scope, element) {
            scope.openDirective = function() {
                scope.open()
                alert("Hello from Directive")
            }
            scope.hello ='dad'            
        }
    };
})
.directive('secondDirective', function(){
    return {
        restrict: 'EA',
        replace: true,
        scope: true,
        transclude: true,
        template: '<div id="holder" data-ng-controller="MyController">{{shouldBeOpen}}<button data-ng-click="openDirective()">{{hello}} Open</button><button data-ng-click="close()">Close</button></div>',
        link: function(scope, element) {
            scope.openDirective = function() {
                scope.open()
                alert("Hello from Directive")
            }
            scope.hello ='dad'            

        }
    };
});;

app.controller('MyController', ['$scope', function($scope) {
    $scope.shouldBeOpen = false
    $scope.close = function() {
        $scope.shouldBeOpen = false
    }
    $scope.open = function() {
        $scope.shouldBeOpen = true
        alert("Hello from Controller")
    }
}]);
4

1 に答える 1

0

$$prevSiblingトランスクルードされたスコープから、ディレクティブによって作成された分離されたスコープへの参照に使用できます。

<button data-ng-click="$$prevSibling.openDirective()">{{hello}} Open</button>
于 2013-09-06T17:19:44.677 に答える