0
내가 틀렸다면 정정하십시오. 그러나 firstDirective
시나리오에서 형제 범위를 만들기 때문에 secondDirective
의 동작을 얻을 수 없다고 생각합니다. 템플릿의 컨트롤러 범위에 액세스 할 수 없습니다. 나는 이식의 힘을 가지고 secondDirective
의 행동을 원합니다. 이것을 달성 할 수있는 방법이 있습니까? 또는이 문제를 잘못된 방법으로 공격하고 있습니까?중계 후 컨트롤러 범위에 어떻게 액세스합니까?
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")
}
}]);