How to use ng-if with ng-disabled

Mohammed picture Mohammed · Jan 20, 2017 · Viewed 23.2k times · Source

Below is my Code.

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.

Help me for this.

Answer

digit picture digit · Jan 20, 2017

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

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