2017-06-02 67 views
1

我在做內聯編輯和更新。
EditCancel功能工作正常。但在更新時無法將更新的字段值傳遞到update函數,其顯示undefinedconsole.log
這裏有什麼問題?如何將更新的字段值傳遞給一個函數(內聯編輯和更新)

<body ng-app="intranet_App"> 
    <table class="table table-hover table-bordered" id="mydata" ng-controller="myCtrl"> 
    <thead class="colorBlue"> 
     <tr> 
     <th>S.No</th> 
     <th>Name</th> 
     <th>Action</th> 
     </tr> 
    </thead> 
    <tbody id=""> 
     <tr ng-repeat="x in roleList | filter:searchText" ng-model="x.Id"> 
     <td>{{x.Id}}</td> 
     <td> 
      <span ng-hide="editMode">{{x.name}}</span> 
      <input type="text" ng-show="editMode" ng-model="x.name" /> 
     </td> 
     <td> 
      <i class="edit fa fa-pencil-square-o" id="edit{{x.Id}}" ng-click="editMode = true;edit(x.Id)" ng-hide="editMode"></i> 
      <i class="update fa fa-floppy-o" id="update{{x.Id}}" ng-hide="true" ng-show="editMode" ng-click="update(x.name);editMode = false"></i> 
      <i class="editCancel fa fa-times" id="editCancel{{x.Id}}" ng-click="editMode = false" ng-hide="true" ng-show="editMode"></i> 
     </td> 
     </tr> 
    </tbody> 
    </table> 
</body> 
<script> 
    var app = angular 
    .module("intranet_App", []) 
    .controller('myCtrl', function($scope, $http) { 
     $scope.updateItem = []; 
     $scope.updatedList = function(val) { 
     $scope.updateItem.push(val); 
     $scope.json = angular.toJson($scope.val); 

     } 
     $http.post("/Admin/getRolesList") 
     .then(function(response) { 
      $scope.roleList = response.data; 
     }); 
     //$scope.edit=function(val){ 
     // $scope.editing = $scope.items.indexOf(val); 
     //} 
     $scope.update = function(val) { 
     console.log(val) 
     $scope.updatedList(val); 
     var requestHeaders = { 
      "Content-type": 'application/json' 
     } 
     var httpRequest = { 
      method: 'post', 
      url: '/Admin/RoleUpdate', 
      headers: requestHeaders 
     }; 
     httpRequest.data = $scope.json; 
     $http(httpRequest).then(function(response) { 
      alert("success") 
     }) 
     } 
     $scope.cancel = function(val) { 
     } 
    }) 
</script> 
+2

在你的更新列表功能,您使用$ scope.val在$ scope.json = angular.toJson($ scope.val);並沒有$ scope.val。將其更改爲$ scope.json = angular.toJson(val);只有它應該工作 –

回答

1

updatedList function,你已經採取$scope.val但它應該是val

因爲你是從功能參數訪問val,它不會在scope所以$scope.val將不確定

變更angular.toJson($scope.val);angular.toJson(val);

$scope.updatedList = function (val) { 
    $scope.updateItem.push(val); 
    $scope.json = angular.toJson($scope.val); 
    console.log($scope.json) 
} 

應該是,

$scope.updatedList = function (val) { 
    $scope.updateItem.push(val); 
    $scope.json = angular.toJson(val); 
    console.log($scope.json) 
} 

更新根據自己的需要:

$scope.updatedList = function (val) { 
    $scope.updateItem.push(val); 
    $scope.json = angular.toJson(val); 
    if($scope.json) 
    { 
     $scope.json = { "name": $scope.json } 
    } 
    console.log($scope.json) 
} 
+0

'更新'函數本身沒有'未定義' – user7397787

+0

未來,但你沒有在'更新'功能的控制檯 – Sravan

+0

你知道了。謝謝。 – user7397787