3

下面是我的代码。

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 来实现这一点。

帮我解决这个问题。

4

2 回答 2

3

你可以这样做

<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>

控制器

提交后只需将禁用属性添加到“Rohan”

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

于 2017-01-20T12:41:12.447 回答
1

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

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

它的意思是在标签 div 中,如果 error == true,则不会在 dom 中加载任何内容;所以 ng-disable 在这种情况下不可用。

于 2017-01-20T11:51:58.257 回答