本文介绍了在angularjs单独切换的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是小提琴:
angular.module("myApp", []) //
.controller('MainController', function($scope) {
$scope.toggle1 = function() {
$scope.$broadcast('event:toggle');
}
$scope.toggle2 = function() {
$scope.$broadcast('event:toggle');
}
}) //
.directive('toggle', function() {
return function(scope, elem, attrs) {
scope.$on('event:toggle', function() {
elem.slideToggle();
});
};
});
所以点击toggle1或toggle2将在同一时刻打开这两个容器。但是,我想单独切换他们。我怎样才能做到这一点?
So clicking on toggle1 or toggle2 will open both container at the same moment. But I want to toggle them independently. How can I achieve this?
推荐答案
您必须做些什么来分隔的事件。您可以在广播提供一些额外的数据:
You have to do something to separate the events. You can provide some additional data in the broadcast:
angular.module("myApp", []) //
.controller('MainController', function($scope) {
$scope.toggle1 = function() {
$scope.$broadcast('event:toggle','1');
}
$scope.toggle2 = function() {
$scope.$broadcast('event:toggle','2');
}
}) //
.directive('toggle', function() {
return function(scope, elem, attrs) {
scope.$on('event:toggle', function(ev,id) {
if(attrs.toggle === id){ elem.slideToggle(); }
});
};
});
<div class="div1" toggle="1">
<p>This is section #1</p>
</div>
<div class="div2" toggle="2">
<p>This is section #2</p>
</div>
请参阅我的叉:
这篇关于在angularjs单独切换的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!