当我点击元素时,代码似乎只隐藏它,但是当我再次单击该元素时,它不再出现。
HTML(AngularJS)
<div ng-click="showIt();">Click Me!</div>
<div style="visible:{{tog}};">Hide Me...Then Show Me Again</div>
控制器:
$scope.showIt=function(){
if($scope.tog="visible"){
$scope.tog="hidden";
}
else{
$scope.tog="visible";
}
}
$scope.tog="visible";
提前致谢:)
答案 0 :(得分:3)
ng-show
指令呢?
<div ng-click="tog = !tog">Click Me!</div>
<div ng-show="tog">Hide Me...Then Show Me Again</div>
答案 1 :(得分:1)
该样式在visibility
条件下也称为if
,您需要使用==
而不是=
。
但更好的解决方案是使用ng-style
指令或使用基于类的解决方案
var app = angular.module('my-app', [], function() {})
app.controller('AppController', function($scope) {
$scope.showIt = function() {
if ($scope.tog.visibility == "visible") {
$scope.tog.visibility = "hidden";
} else {
$scope.tog.visibility = "visible";
}
}
$scope.showIt2 = function() {
$scope.hidden = !$scope.hidden;
}
$scope.tog = {
visibility: "visible"
};
$scope.hidden = false;
})
.hidden {
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="my-app">
<div ng-controller="AppController">
<div ng-click="showIt()">Click Me!</div>
<div ng-style="tog">Hide Me...Then Show Me Again</div>
<hr />
<div ng-click="showIt2()">Click Me!</div>
<div ng-class="{hidden: hidden}">Hide Me...Then Show Me Again</div>
</div>
</div>
答案 2 :(得分:0)
您也可以使用ng-if
指令执行此操作:
<div ng-click="showIt();">Click Me!</div>
<div ng-if="tog">Hide Me...Then Show Me Again</div>
JavaScript的:
angular.module('myApp', [])
.controller('MyController', function($scope) {
$scope.tog = true;
$scope.showIt=function(){
$scope.tog=!$scope.tog
}
});