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 回答