如何使用ng-if和ng-disabled

时间:2017-01-20 11:42:05

标签: angularjs angularjs-ng-repeat angular-ng-if angularjs-ng-disabled

以下是我的代码。

var myfriend = angular.module('myfriend',[]);

myfriend.controller('myfriendController', function($scope) 
{
   $scope.record = [
       {     "id" : "01",
            "firstname" : "Mohan ",
            "middlename" : "K",
            "lastname" : "Futterkiste1"
        },{
             "id" : "04",
            "firstname" : "Rohan ",
            "middlename" : "A",
            "lastname" : "Futterkiste2"
        },{
              "id" : "08",
            "firstname" : "sohan ",
            "middlename" : "M",
            "lastname" : "Futterkiste3"
        }
   ]
  
                   
});
<html>
  <head>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

  </head>
  <body ng-app="myfriend">
    
        
    
    <table class="table" style="border:1px red solid; width:100%; "  ng-controller="myfriendController">
		    <thead>
		      <tr>
		      	<th>Id</th>
		        <th>First name</th>
		        <th>Middle name</th>
   		        <th>Last name</th>
		      </tr>
		    </thead>
		    <tbody>
		      <tr ng-repeat="x in record">
   		        <th>{{x.id}}</th>
   		        <th><input type="text" ng-value="x.firstname"></th>
                <th><input type="text" ng-value="x.middlename"></th>
                <th><input type="text" ng-value="x.lastname"></th>
		      </tr>
              <tr>
                <td><button> Submit</button></td>
              </tr>
		    </tbody>  
	</table> 
  <body>
</html>

我想要的是点击提交按钮,它应该仅禁用Rohan的字段(Rohan正在通过ng-repeat进行)。我想通过ng-if中的ng-disabled实现此目的。

帮助我。

2 个答案:

答案 0 :(得分:2)

你可以这样做

<tr ng-repeat="x in record">
   <th>{{x.id}}</th>
   <th><input type="text" ng-model="x.firstname" ng-disabled="x.disabled" value="x.firstname"></th>
   <th><input type="text" ng-model="x.middlename"  value="x.middlename"></th>
   <th><input type="text" ng-model="x.lastname" value="x.lastname"></th>
</tr>

<强>控制器

只需将已停用的属性添加到&#39; Rohan&#39;提交后

var myfriend = angular.module('myfriend',[]);

myfriend.controller('myfriendController', function($scope) 
{

   $scope.record = [
       {     "id" : "01",
            "firstname" : "Mohan",
            "middlename" : "K",
            "lastname" : "Futterkiste1"
        },{
             "id" : "04",
            "firstname" : "Rohan",
            "middlename" : "A",
            "lastname" : "Futterkiste2"
        },{
              "id" : "08",
            "firstname" : "sohan",
            "middlename" : "M",
            "lastname" : "Futterkiste3"
        }
   ]

   $scope.submit = function () {
       angular.forEach($scope.record, function (v) {
          if (v.firstname === 'Rohan') {
              v.disabled = true;
           }
       });
       console.log($scope.record);
   }


});

工作小提琴:JsFiddle

答案 1 :(得分:1)

我认为,事情是不可能的,因为ng-if删除了html标签。像

<div ng-if="error">
      <input type="text" ng-disabled="disable">
</div>

它的意思是在标签div内,如果错误== true,则无法在dom中加载; 所以在这种情况下,ng-disable不可用。