A
A
Andrey2019-01-05 03:47:56
JavaScript
Andrey, 2019-01-05 03:47:56

AngularJS. Issues with ng-disabled in ng-repead loop. How to select a specific line for editing?

When you click on the pencil (last column), the full name (first column) of this line should be edited.
View:

<body ng-controller="indexController">
<div class="col-sm-6">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>ФИО</th>
            <th>Год рождения</th>
            <th>***</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="item in items">
            <td><input type="text" ng-disabled="disabled.$index" ng-init="disabled.$index=true" value="{{item[0]}}"></td>
            <td>{{item[1]}}</td>
            <td><span class="fas fa-pencil-alt" ng-click="edit($index)"></span></td>
        </tr>
        </tbody>
    </table>
</div>

Controller:
testApp.controller('indexController', function ($scope, $http){
        $http({method: 'POST', url: 'http://public/rest_api/index'}).
        then(function success(response) {
            $scope.items = response.data;

        });
        $scope.edit = function (index) {
            console.log(index);
            $scope.disabled = false;
        }
    });

Tried like this, doesn't work:
$scope.disabled.index = false;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-01-05
@DronTat

Why such difficulties? Let you have the index of the element being edited, and that's it:

$scope.edit = function(index) {
  $scope.active = index;
};

ng-disabled="$index !== active"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question