根据true或false将值推送到数组中

时间:2016-10-28 11:55:49

标签: angularjs arrays

如果glyphicon close $scope.fnamea为真,我需要将$scope.lnamea值作为名字,将$scope.snamea作为姓氏,将$scope.afname作为姓氏。我尝试过这样做但每次我的值都只是名字,因为我的名字已经$scope.afname为True,因为数组允许重复。

HTML:

<div class="row">                            
    <input  type="text" name="fname" ng-model="model.firstName" "/>    
    <a class="menu-toggle" ng-click="hiddenFields(afname = afname?false:true)" class="btn btn-default" ng-model="model.fnamea">
    <i class="glyphicon" ng-class="{'glyphicon-eye-open':!afname, 'glyphicon-eye-close':afname}"></i></a>
 </div>  

<div class="row">
    <div>                      
        <input  type="text" name="sname" ng-model="model.surname" "/>
        <a class="menu-toggle" ng-click="hiddenFields(sfname = sfname?false:true)" class="btn btn-default" ng-model="model.snamea">
        <i class="glyphicon" ng-class="{'glyphicon-eye-open':!sfname, 'glyphicon-eye-close':sfname}"></i></a>
    </div>  
    <div>

        <input  type="text" name="lname" ng-model="model.lastName" "/>

        <a class="menu-toggle" ng-click="hiddenFields(lfname = alname?false:true)" class="btn btn-default" ng-model="model.lnamea">
        <i class="glyphicon" ng-class="{'glyphicon-eye-open':!alname, 'glyphicon-eye-close':alname}"></i></a>
    </div>                        
</div>

JS:

<script>$('.menu-toggle').click( function(){
    $(this).find('i').toggleClass('glyphicon glyphicon-eye-open').toggleClass('glyphicon glyphicon-eye-close');
});</script>
$scope.fnamea = "firstname";
$scope.lnamea = "lastname";
$scope.snamea ="shortname";

$scope.hiddenFields = function(){

  if($scope.afname == true) {
                    $scope.hide.push($scope.fnamea);
                    console.log($scope.hide);
                arr[index]++; 
                }

         else  if ($scope.alname == true) {
                    $scope.hide.push("lastname");
                    console.log($scope.hide);
                }
             else  if ($scope.asname == true) {
                    $scope.hide.push("surname");
                    console.log($scope.hide);
                }
            }

            if($scope.adob == "true") {
                    $scope.hide.push("dob");
                    console.log($scope.hide);
                }
};

1 个答案:

答案 0 :(得分:1)

我希望你能找到这样的东西:

  <input  type="text" ng-model="user.fname" ng-disabled="!allow.fname"/>  
  <input  type="checkbox" ng-model="allow.fname" /> 
  <hr/>
  <input  type="text" ng-model="user.lname" ng-disabled="!allow.lname"/>  
  <input  type="checkbox" ng-model="allow.lname" /> 
  <hr/>
  <input  type="text" ng-model="user.sname" ng-disabled="!allow.sname"/>  
  <input  type="checkbox" ng-model="allow.sname" /> 

function MyCtrl($scope) {
    $scope.user = {
      fname: 'First name',
      lname: 'Last name',
      sname: 'Surname'
    };

    $scope.allow = {
      fname : true,
      lname : true,
      sname : true,
    };

    $scope.users = [];

    $scope.push = function(){
        var user = {}, 
            allow = $scope.allow;
      Object.keys(allow).forEach(function(key){
        allow[key] ? user[key] = $scope.user[key] : null;
      });
      $scope.users.push(user);
    } 
}

Fiddle example

谢谢!