如何在AngularJS中使用ng-show和ng-hide按钮

时间:2014-08-01 07:05:24

标签: html angularjs

我有两个按钮Start和Stop,当我点击Start按钮时,我想要显示Stop按钮,而当我点击Stop按钮时,我想要显示Start按钮,我希望显示Start按钮和Stop按钮是隐藏的。任何时候都只能显示一个按钮。这就是我尝试的但是它似乎无法工作。我哪里出错了?

<span ng-hide="Model.StartEvent == true">
    <button ng-click="Model.StartEvent()" id="btnEventStart">Start</button>
</span>

<span ng-show="Model.StopEvent == false">
    <button ng-click="Model.StopEvent()" id="btnStopEvent" class="tooltip">Stop</button>
</span>

3 个答案:

答案 0 :(得分:4)

你可以做得更少

<button ng-click="goEvent();" ng-hide="going">Start</button>
<button ng-click="goEvent();" ng-show="going">Stop</button>

...

$scope.going = false;

$scope.goEvent = function(){
   $scope.going = !$scope.going;
   if($scope.going){  
       $scope.go();
   }else{
       $scope.stop();
   }      
}

答案 1 :(得分:2)

你应该为两个使用相同的变量...

<button ng-click="Model.toggleShow(); Model.StartEvent()" ng-show="Model.showStartEvent">Start</button>

<button ng-click="Model.toggleShow(); Model.StopEvent()" ng-show="!Model.showStartEvent">Stop</button>


Modal.toggleShow = function(){
  Model.showStartEvent = !Model.showStartEvent;
}

你也可以在startevent / stopevent中调用切换,而不是在ng-click上调用两个函数

答案 2 :(得分:0)

<强> HTML

<div ng-controller="YourController">  
<span ng-hide="EventRunning"><button ng-click="StartEvent($event)"id="btnEventStart">Start</button></span> 
<span ng-show="EventRunning"><button ng-click="StopEvent($event)" id="btnStopEvent">Stop</button></span>  
</div>

使用单个信号量进行隐藏并显示 EventRunning

<强>控制器:

{ ...
     $scope.EventRunning = false;
     $scope.StartEvent = function (event) {
            event.preventDefault();
            $scope.EventRunning = true;
            // your code 
    }

    $scope.StopEvent = function (event) {
            event.preventDefault();
            $scope.EventRunning = false;
            // your code 
    } 
... }