2016-12-15 18 views
1

管理者権限を持っています - ユーザーを編集するには\ delete \を追加できます。これはhtmlの一部であり、何も重要ではありませんが、確かに(ng-controllerとng-appはインデックスファイルに含まれています)。angular-jsは変更を保存できません

<div class="row" ng-repeat="user in users"> 
      <form name="myForm2"> 
       <div class="col-md-1 cell"> 
        <span ng-show="!editMode">{{user.username}}</span> 
        <input ng-show="editMode" type="text" name="username" ng-model="current.username" required > 
       </div> 
       <div class="col-md-1 cell"> 
        <span ng-show="!editMode">{{user.firstname}}</span> 
        <input ng-show="editMode" type="text" name="firstname" ng-model="current.firstname" required> 
       </div> 
       <div class="col-md-1 cell"> 
        <span ng-show="!editMode">{{user.lastname}}</span> 
        <input ng-show="editMode" type="text" name="lastname" ng-model="current.lastname" required> 
       </div> 
       <div class="col-md-1 cell"> 
        <span ng-show="!editMode">{{user.age}}</span> 
        <input ng-show="editMode" type="number" name="age" ng-model="current.age" required> 
       </div> 
       <div class="col-md-2 cell"> 
        <span ng-show="!editMode">{{user.email}}</span> 
        <input ng-show="editMode" type="email" name="email" ng-model="current.email" required> 
       </div> 
      </form> 
      <div class="col-md-3 cell"> 
       <button type="button" class="btn btn-warning" ng-show="!button1" ng-click="edit(user); userEddit = !userEddit; button1 = !button1; editMode = !editMode">Eddit</button> 
       <span ng-show="userEddit"> 
        <h6>Save changes?<br> 
         <button type="submit" class="btn btn-success btn-xs col-md-offset-1" ng-click="save(user); userEddit = !userEddit; button1 = !button1; editMode = !editMode" ng-disabled="myForm2.$invalid">Save</button> 
         <button type="button" class="btn btn-danger btn-xs col-md-offset-1" ng-click="cancel(user); userEddit = !userEddit; button1 = !button1; editMode = !editMode">Cancel</button> 
        </h6> 
       </span> 
      </div> 
      <div class="col-md-3 cell"> 
       <button type="button" class="btn btn-danger" ng-show="!button" ng-click="userDelete = !userDelete; button = !button">Del</button> 
       <span ng-show="userDelete"> 
        <h6>Sure? 
         <button type="submit" class="btn btn-success btn-xs col-md-offset-1" ng-click="removeRow(user.username)">Del</button> 
         <button type="button" class="btn btn-danger btn-xs col-md-offset-1" ng-click="userDelete = !userDelete; button = !button">Cancel</button> 
        </h6> 
       </span> 
      </div> 
     </div> 

controllers.js

'use strict'; 
var App = angular.module('App', []); 
App.controller('ListCtrl', function($scope) { 
    $scope.users = [ 
    {"username":"user1","firstname":"John","lastname":"Anderson","age":25,"email":"[email protected]"}, 
    {"username":"user2","firstname":"Paul","lastname":"Winfred","age":27,"email":"[email protected]"}, 
    {"username":"user3","firstname":"Adan","lastname":"Kelley","age":64,"email":"[email protected]"}, 
    {"username":"user4","firstname":"Vernon","lastname":"McCall","age":70,"email":"[email protected]"}, 
    {"username":"user5","firstname":"Ernest","lastname":"Heflin","age":38,"email":"[email protected]"}, 
    {"username":"user6","firstname":"John","lastname":"Barton","age":52,"email":"[email protected]"}, 
    {"username":"user7","firstname":"Jessica","lastname":"Lara","age":25,"email":"[email protected]"} 
    ]; 

$scope.removeRow = function(username){    
    var index = -1;  
    var comArr = eval($scope.users); 
    for(var i = 0; i < comArr.length; i++) { 
     if(comArr[i].username === username) { 
      index = i; 
      break; 
     } 
    } 
    if(index === -1) { 
     alert("Something gone wrong"); 
    } 
    $scope.users.splice(index, 1);   
}; 

$scope.current = {}; 
$scope.copy = {}; 

$scope.edit = function(user){ 
    $scope.copy = angular.copy(user); 
    $scope.current = angular.copy(user);  
}; 

$scope.save = function(user){ 
    user = $scope.current; 
    $scope.current = {}; 
    $scope.copy = {}; 
}; 

$scope.cancel = function(user) { 
    user = $scope.copy; 
    $scope.current = {}; 
    $scope.copy = {}; 
}; 
}); 

私の問題があります - 私は、変更を保存することができません。 しかし、私は

$scope.edit = function(user){ 
     $scope.copy = angular.copy(user); 
     $scope.current = angular.copy(user);  
    }; 

    $scope.save = function(user){ 
     $scope.users[0] = $scope.current; 
     $scope.current = {}; 
     $scope.copy = {}; 
    }; 

    $scope.cancel = function(user) { 
     $scope.users[0] = $scope.copy; 
     $scope.current = {}; 
     $scope.copy = {}; 
    }; 

それは最初の一つだけのために働くことを行った場合。

答えて

0
user = $scope.current; 

これは問題です。あなたのメソッドには問題があると思います。たとえば、次のように使用して保存してください。

$scope.users.push(user); 

これはユーザをユーザ配列に追加します。

+0

申し訳ありませんが、私の英語は完璧ではないサイクルでの一致を確認しました。私は変更を保存できない – Rust

0

見つかり答え:ちょうど私のデータベースにIDを作る

for (var i = 0; i < $scope.users.length; i++) { 
    $scope.users[i].id = i; 
    } 

は、それからちょうど

$scope.edit = function(user){ 
    $scope.copy = angular.copy(user); 
    $scope.current = angular.copy(user);  
}; 

$scope.save = function(user){ 
    for (var i = 0; i < $scope.users.length; i++) { 
     if ($scope.users[i].id === $scope.current.id){ 
      $scope.users[i]=$scope.current; 
     } 
    } 
    $scope.current = {}; 
    $scope.copy = {}; 
}; 

$scope.cancel = function(user) { 
    for (var i = 0; i < $scope.users.length; i++) { 
     if ($scope.users[i].id === $scope.copy.id){ 
      $scope.users[i]=$scope.copy; 
     } 
    } 
    $scope.current = {}; 
    $scope.copy = {}; 
}; 

おかげでとにかく

関連する問題