嗨我有兩個彈出式指令在同一頁上。問題是當我點擊一個他們都彈出。不同範圍的多個AngularJS指令
如何隔離每個範圍,以便只彈出被點擊的彈出窗口?
HTML
<popup class="popup">
<trigger>
<a href="#" data-ng-click="showPopup()">Show Popup</a>
</trigger>
<pop>
I popped up
</pop>
</popup>
<popup class="popup">
<trigger>
<a href="#" data-ng-click="showPopup()">Show Popup</a>
</trigger>
<pop>
I popped up too
</pop>
</popup>
popup.js
angular.module('sembaApp')
.directive('popup', function() {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div data-ng-transclude></div>',
controller: function postLink($scope, $element, $attrs) {
$scope.popup = false;
$scope.showPopup = function() {
$scope.popup = !$scope.popup;
}
}
}
})
.directive('trigger', function() {
return {
require: '^popup',
restrict: 'E',
replace: true,
transclude: true,
template: '<div data-ng-transclude></div>',
}
})
.directive('pop', function() {
return {
require: '^popup',
restrict: 'E',
replace: true,
transclude: true,
template: '<aside data-ng-transclude data-ng-show="popup"> yay</aside>'
}
});
是的這個作品了。唯一的是我不喜歡使用ng-init,因爲它在我看來是邏輯。 – otissv
好吧,您可以將該初始化邏輯移動到控制器。我只是想演示代碼並簡化一下。 – zsong