如何隐藏点击按钮1,并显示BUTTON2并点击按钮2隐藏按钮2和按钮1显示在angularjs?

问题描述 投票:1回答:4

我试图隐藏按钮1,当用户点击按钮1,并显示button2.when用户点击按钮2,并显示Button1的。在当用户点击按钮1调用函数myController1和时间,当用户点击按钮2调用函数myController2

<!DOCTYPE html>
<html>
<body>

<div ng-app="" ng-controller="myController">

<button ng-click="test1=true;myController1()" ng-show="test1">button1</button>

<button ng-show="test2" ng-click="test1=true;myController2()">button2</button>

</div>
<script>
function myController1() {
    alert("hello");
}
function myController2() {
    alert("hiii");
}
</script> 

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> 

</body>
</html>

如何表现等因素,也叫控制器

javascript angularjs-directive
4个回答
0
投票

你可以做那么突兀和代码更清晰,如果你使用只有一个变量来控制显示/隐藏。

<button ng-click="click1()" ng-show="button==1">button1</button>
<button ng-click="click2()" ng-show="button==2">button2</button>

控制器:

$scope.button = 1;
$scope.click1 = function() {
    alert(1);
    $scope.button = 2;
};
$scope.click2 = function() {
    alert(2);
    $scope.button = 1;
};

Demo: http://plnkr.co/edit/uZNwVDFWZyYvLFh2KgSQ?p=preview


0
投票

显示,如果变量=真对按钮1,然后显示,如果变量= false为按钮2。

<button ng-click="toggleButtons()" ng-show="showButton1">button1</button>

<button ng-click="toggleButtons()" ng-show="!showButton1">button2</button>

然后在你的控制器

$scope.showButton1 = true;

$scope.toggleButtons = function () {
  $scope.showButton1 = !$scope.showButton1;
};

或者(取决于你为什么需要这样),你可以仅仅通过其绑定到您更改上点击一个变量更改按钮中的文本。

<button ng-click="toggleButtons()" ng-show="showButton1">{{buttonText}}</button>

0
投票

你应该定义相应控制器这些功能。你所提到的NG-控制器,在定义这些myController的功能。

见例如

xxxx.controller('myController', function($scope) {
    $scope.myController1 = function() {
        alert("hello");
    }
    $scope.myController2 = function() {
        alert("hiiii");
    }
});

0
投票
<div ng-app="app" ng-controller="demo">
  <button ng-click='btn1();' ng-show="show_btn1">btn1</button>
  <button ng-click='btn2();' ng-show="show_btn2">btn2</button>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script> 
<script>
angular.module('app', []).controller('demo', function demo($scope) {
  $scope.show_btn1 = true;
  $scope.show_btn2 = false;

  $scope.btn1 = function() {
    $scope.show_btn1 = false;
    $scope.show_btn2 = true;
  };

  $scope.btn2 = function() {
    $scope.show_btn1 = true;
    $scope.show_btn2 = false;
  };
})
</script>
© www.soinside.com 2019 - 2024. All rights reserved.