我在做內聯編輯和更新。
Edit
和Cancel
功能工作正常。但在更新時無法將更新的字段值傳遞到update
函數,其顯示undefined
在console.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>
在你的更新列表功能,您使用$ scope.val在$ scope.json = angular.toJson($ scope.val);並沒有$ scope.val。將其更改爲$ scope.json = angular.toJson(val);只有它應該工作 –