如何在禁用ng的情况下使用ng-if

时间:2021-08-06 19:42:18

Below is my Code.

下面是我的代码。

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>

What I want is on click of submit button, it should disabled the field of Rohan only(Rohan is coming through ng-repeat).I want to achieve this through ng-disabled in ng-if.

我想要的是点击提交按钮,它应该只禁用Rohan的字段(Rohan是通过ng-repeat来的)。我想通过ng-if的ng-disabled来实现这个目标。

Help me for this.

帮助我。

2 个解决方案

#1


2  

You can by doing this

你可以这样做

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

Controller

控制器

Just add disabled attribute to 'Rohan' after submit

只需在提交后添加禁用属性到“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);
   }


});

Working fiddle: JsFiddle

工作小提琴:JsFiddle

#2


0  

I think, thing is not possible, because ng-if delete the html tags. Like

我认为,这是不可能的,因为ng-如果删除html标签。就像

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

its means inside tag div nothing will load in dom if error == true; so ng-disable is no available in this case.

它的意思是,在tag div中,如果error = true, dom中没有东西会被加载;所以在这种情况下,ng-disable是无效的。

#1


2  

You can by doing this

你可以这样做

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

Controller

控制器

Just add disabled attribute to 'Rohan' after submit

只需在提交后添加禁用属性到“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);
   }


});

Working fiddle: JsFiddle

工作小提琴:JsFiddle

#2


0  

I think, thing is not possible, because ng-if delete the html tags. Like

我认为,这是不可能的,因为ng-如果删除html标签。就像

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

its means inside tag div nothing will load in dom if error == true; so ng-disable is no available in this case.

它的意思是,在tag div中,如果error = true, dom中没有东西会被加载;所以在这种情况下,ng-disable是无效的。