Javascript 如果另一个开关打开,确保开关关闭
有两个不同的div,可以在Angualar应用程序中切换打开和关闭,但要确保一个div打开,另一个div关闭。似乎这在NG中应该足够简单,但在Angular中仍然是新的。有人有什么建议吗 在这里做了一个提琴的例子: 以下是示例:Javascript 如果另一个开关打开,确保开关关闭,javascript,angularjs,toggle,Javascript,Angularjs,Toggle,有两个不同的div,可以在Angualar应用程序中切换打开和关闭,但要确保一个div打开,另一个div关闭。似乎这在NG中应该足够简单,但在Angular中仍然是新的。有人有什么建议吗 在这里做了一个提琴的例子: 以下是示例: <body ng-app="simpleToggle"> <div ng-controller="AppCtrl"> <button ng-click="toggleCustom1()">Custom</but
<body ng-app="simpleToggle">
<div ng-controller="AppCtrl">
<button ng-click="toggleCustom1()">Custom</button>
<span ng-hide="custom1">
<h2>Custom 1 is showing but Custom 2 should not be if it was already opened</h2>
</span>
<span ng-show="custom1"></span>
</div>
<div ng-controller="App2Ctrl">
<button ng-click="toggleCustom2()">Custom2</button>
<span ng-hide="custom2">
<h2>Custom 2 is showing but Custom 1 should not be if it was already opened.</h2>
</span>
<span ng-show="custom2"></span>
</div>
</body>
angular.module('simpleToggle', [])
.controller('AppCtrl',['$scope', function($scope){
$scope.custom1 = true;
$scope.toggleCustom1 = function() {
$scope.custom1 = $scope.custom1 === false ? true: false;
};
}])
.controller('App2Ctrl',['$scope', function($scope){
$scope.custom2 = true;
$scope.toggleCustom2 = function() {
$scope.custom2 = $scope.custom2 === false ? true: false;
};
}]);
习俗
自定义1正在显示,但如果自定义2已打开,则不应显示
习惯2
自定义2正在显示,但如果自定义1已打开,则不应显示。
angular.module('simpleToggle',[])
.controller('AppCtrl',['$scope',函数($scope){
$scope.custom1=true;
$scope.toggleCustom1=函数(){
$scope.custom1=$scope.custom1===false?true:false;
};
}])
.controller('App2Ctrl',['$scope',函数($scope){
$scope.custom2=true;
$scope.toggleCustom2=函数(){
$scope.custom2=$scope.custom2===false?true:false;
};
}]);
在这里,您处理的是范围层次结构,您需要使用其中一种机制来协调控制器之间的关系。有些选择是:
<body ng-app="simpleToggle">
<div ng-controller="OuterCtrl">
<div ng-controller="AppCtrl">
<button ng-click="toggleCustom1()">Custom</button>
<span ng-hide="!custom1">
<h2>Custom 1 is showing but Custom 2 should not be if it was already opened</h2>
</span>
</div>
<div ng-controller="App2Ctrl">
<button ng-click="toggleCustom2()">Custom2</button>
<span ng-hide="!custom2">
<h2>Custom 2 is showing but Custom 1 should not be if it was already opened.</h2>
</span>
</div>
</div>
</body>
现在这种特殊的技术只适用于少数需要协调的事情。如果您有大量这些需要协调的事情,那么消息或服务可能会更好。另一种选择是将它们全部放在同一个控制器中。在处理范围层次结构时,您需要使用其中一种机制来协调控制器之间的关系。有些选择是:
<body ng-app="simpleToggle">
<div ng-controller="OuterCtrl">
<div ng-controller="AppCtrl">
<button ng-click="toggleCustom1()">Custom</button>
<span ng-hide="!custom1">
<h2>Custom 1 is showing but Custom 2 should not be if it was already opened</h2>
</span>
</div>
<div ng-controller="App2Ctrl">
<button ng-click="toggleCustom2()">Custom2</button>
<span ng-hide="!custom2">
<h2>Custom 2 is showing but Custom 1 should not be if it was already opened.</h2>
</span>
</div>
</div>
</body>
现在这种特殊的技术只适用于少数需要协调的事情。如果您有大量这些需要协调的事情,那么消息或服务可能会更好。另一种选择是将它们全部放在同一个控制器中。在处理范围层次结构时,您需要使用其中一种机制来协调控制器之间的关系。有些选择是:
<body ng-app="simpleToggle">
<div ng-controller="OuterCtrl">
<div ng-controller="AppCtrl">
<button ng-click="toggleCustom1()">Custom</button>
<span ng-hide="!custom1">
<h2>Custom 1 is showing but Custom 2 should not be if it was already opened</h2>
</span>
</div>
<div ng-controller="App2Ctrl">
<button ng-click="toggleCustom2()">Custom2</button>
<span ng-hide="!custom2">
<h2>Custom 2 is showing but Custom 1 should not be if it was already opened.</h2>
</span>
</div>
</div>
</body>
现在这种特殊的技术只适用于少数需要协调的事情。如果您有大量这些需要协调的事情,那么消息或服务可能会更好。另一种选择是将它们全部放在同一个控制器中。在处理范围层次结构时,您需要使用其中一种机制来协调控制器之间的关系。有些选择是:
<body ng-app="simpleToggle">
<div ng-controller="OuterCtrl">
<div ng-controller="AppCtrl">
<button ng-click="toggleCustom1()">Custom</button>
<span ng-hide="!custom1">
<h2>Custom 1 is showing but Custom 2 should not be if it was already opened</h2>
</span>
</div>
<div ng-controller="App2Ctrl">
<button ng-click="toggleCustom2()">Custom2</button>
<span ng-hide="!custom2">
<h2>Custom 2 is showing but Custom 1 should not be if it was already opened.</h2>
</span>
</div>
</div>
</body>
现在这种特殊的技术只适用于少数需要协调的事情。如果您有大量这些需要协调的事情,那么消息或服务可能会更好。另一种选择是将它们全部放在同一个控制器中。使用指令处理DOM内容是一种很好的做法。我使用
.next()
获取下一个span。或者您可以使用其他选择器来获取它
elem的文档:
在这里工作
html:
指示
app.directive('changeToggle', ['$location', function($location) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function(event) {
var spanner = elem.next();
if(spanner.hasClass("toggle-show-css")) {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-show-css");
spanner.addClass("toggle-hide-css");
} else {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-hide-css");
spanner.addClass("toggle-show-css");
}
});
}
}
}]);
使用指令处理DOM文件是一种很好的做法。我使用
.next()
获取下一个span。或者您可以使用其他选择器来获取它
elem的文档:
在这里工作
html:
指示
app.directive('changeToggle', ['$location', function($location) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function(event) {
var spanner = elem.next();
if(spanner.hasClass("toggle-show-css")) {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-show-css");
spanner.addClass("toggle-hide-css");
} else {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-hide-css");
spanner.addClass("toggle-show-css");
}
});
}
}
}]);
使用指令处理DOM文件是一种很好的做法。我使用
.next()
获取下一个span。或者您可以使用其他选择器来获取它
elem的文档:
在这里工作
html:
指示
app.directive('changeToggle', ['$location', function($location) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function(event) {
var spanner = elem.next();
if(spanner.hasClass("toggle-show-css")) {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-show-css");
spanner.addClass("toggle-hide-css");
} else {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-hide-css");
spanner.addClass("toggle-show-css");
}
});
}
}
}]);
使用指令处理DOM文件是一种很好的做法。我使用
.next()
获取下一个span。或者您可以使用其他选择器来获取它
elem的文档:
在这里工作
html:
指示
app.directive('changeToggle', ['$location', function($location) {
return {
restrict: 'A',
link: function(scope, elem, attrs) {
elem.bind('click', function(event) {
var spanner = elem.next();
if(spanner.hasClass("toggle-show-css")) {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-show-css");
spanner.addClass("toggle-hide-css");
} else {
elem.parent().parent().find('span').removeClass("toggle-show-css");
elem.parent().parent().find('span').addClass("toggle-hide-css");
spanner.removeClass("toggle-hide-css");
spanner.addClass("toggle-show-css");
}
});
}
}
}]);
完美的男人。现在我只有这两个,但它们分别位于使用ui路由器的两个独立视图和多个页面/视图中。非常感谢。在我的应用程序中,虽然我没有主控制器(outerCtrl),但我可能需要将其绑定到全局运行或配置。运行良好,无需包装外部控制器即可完成。再次感谢。。。我可怜的jQuery头脑再次崩溃!下面是一个没有外部控制器的示例,在服务器上使用服务。如果我需要扩展它,可能会使用这个例子。再次感谢,很好!感谢@unobf发布此解决方案。我删除了我的答案,因为这更干净。完美的男人。现在我只有这两个,但它们分别位于使用ui路由器的两个独立视图和多个页面/视图中。非常感谢。在我的应用程序中,虽然我没有主控制器(outerCtrl),但我可能需要将其绑定到全局运行或配置。运行良好,无需包装外部控制器即可完成。再次感谢。。。我可怜的jQuery头脑再次崩溃!这里有一个例子