簡體   English   中英

將值從對象數組復制到AngularJS中的輸入字段

[英]Copy values from object array to input fields in AngularJS

我有一個像這樣的數組:

[
 {
  name: "simon",
  surname: "Bi"
 },
 {
  name: "Frank",
  surname: "Bour"
 }
]

我將所有數據打印在一個表中,我想編輯以下值:

name1 - surname1 - edit1 - remove1
name2 - surname2 - edit2 - remove2

當我按“編輯”時,我想將用戶的數據復制到輸入字段(所以只有2個字段,即姓名和姓氏),以便更改數據和更新數組,但是我不知道如何在AngularJS中做到這一點。

JS:

angular.
   module('peopleList').
   component('peopleList', {
      templateUrl: "list.template.html",
      controller:
            function peopleListController(){
                var self = this;

                self.people = [
                    {
                        name: "Simon",
                        surname: "Bo",
                    }
                ];

                //add
                self.addPerson = function(itemToAdd) {
                    this.people.push(angular.copy(itemToAdd))
                }

                //remove
                self.delete = function(item) {
                    var index = this.people.indexOf(item);
                    this.people.splice(index, 1);
                }

                //edit
                self.edit = function(item){
                code
                }
            }
    });

HTML:

<form name="myForm" ng-submit="$ctrl.addText(form)">
            <div class="form-group">
                <label for="name">Name: </label>
                <input id="name" type="text" class="form-control" ng-model="itemToAdd.name" placeholder="name" required>
            </div>
            <div class="form-group">
                <label for="surname">Surname:</label>
                <input id="surname" type="text" class="form-control" ng-model="itemToAdd.surname" placeholder="surname" required>
               <button ng-click="$ctrl.addPerson(itemToAdd)" class="btn btn-primary" id="add">Add</button>
            </div>
</form>


<table>
            <thead>
                <tr>
                    <td>Name</td>
                    <td>Surname</td>
                    <td colspan="2">Actions</td>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="person in $ctrl.people | filter:$ctrl.query | orderBy: $ctrl.orderProp">
                    <td>{{person.name}}</td>
                    <td>{{person.surname}}</td>
                    <td class="edit" ng-click="$ctrl.edit(person)" style="cursor: pointer; text-decoration: underline;">Edit</td>
                    <td class="remove" ng-click="$ctrl.delete(person)" style="cursor: pointer; font-weight: bold; color: red;">X</td>
                </tr>
            </tbody>
        </table>

嘗試下面的代碼,這幾乎是不言自明的,只是使用ng-model and ng-hide/ng-show來實現。

 var app = angular.module('app',[]); app.controller('Ctrl',function($scope,$filter){ $scope.editField={}; $scope.edit = function(index){ $scope.editField[index] = !$scope.editField[index] ; }; $scope.data = [{name: "simon", surname: "Bi" }, {name: "Frank", surname: "Bour" }]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" class="widget-content" ng-controller="Ctrl"> <table> <tr ng-repeat="emp in data"> <td>{{$index+1}} </td> <td ng-hide="editField[$index]"> {{emp.surname}}</td> <td ng-hide="editField[$index]">{{emp.name}} </td> <td ng-show="editField[$index]"><input type="text" ng-model="emp.surname"/></td> <td ng-show="editField[$index]"><input type="text" ng-model="emp.name"/></td> <td ng-click="edit($index)"><button>{{editField[$index]? 'Save': 'Edit'}}</button></td> </tr> </table> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM