我可以检查条件并在ng-click中更改变量的值

问题描述 投票:1回答:2
ng-click="(document.getElementById('procheck1').checked)?showme=true:showme=false"

(要么)

ng-click="if(document.getElementById('procheck1').checked){showme=true;}else{showme=false;}"
angularjs
2个回答
0
投票

使用这样的函数名称,ng-click =“myFunction()”并在其中写入整个条件。

  myFunction(){
    if(document.getElementById('procheck1').checked) {
      showme=true;
    } else {
      showme=false;
    }
  }

0
投票

在html页面中尝试这个

<div ng-app="MyApp" ng-controller="MyController">
        <label for="chkPassport">
            <input type="checkbox" id="chkPassport" ng- model="ShowPassport" ng-change="ShowHide()" />
            Do you have Passport?
        </label>
        <hr />
        <div ng-show="IsVisible">
            Passport Number:
            <input type="text" id="txtPassportNumber" />
        </div>
    </div>   

而这部分在控制器中

var app = angular.module('MyApp', [])
  app.controller('MyController', function ($scope) {
            //This will hide the DIV by default.
            $scope.IsVisible = false;
            $scope.ShowHide = function () {
                //If DIV is visible it will be hidden and vice versa.
                $scope.IsVisible = $scope.ShowPassport;
            }
        });  
© www.soinside.com 2019 - 2024. All rights reserved.