当我尝试从表中修改一行时,我遇到了问题,我使用AngularJS作为客户端,ASP.Net作为其余的API,这是我的HTML代码:我不能应用从表格行中选择我的数据的修改
<table ng-controller="etudmodifCtrl">
<thead>
</thead>
<tbody >
<tr ng-repeat="store in currentPageStores>
<td align="center">{{store.LastName}}</td>
<td align="center">{{store.FirstName}}</td>
<td align="center">{{store.Email}}</td>
<td align="center" >{{store.Id}}</td>
<td align="center" ng-controller="etuddeleteCtrl">
<div id="myModal" class="modal fade" role="dialog" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">....</div>
<div class="modal-body">
<div class="form-group">
<label>LastName</label>
<div class="col-sm-10">
<input type="text" class="form-control" ng-model="nomet">
</div>
</div>
<div class="form-group">
<label>FirstName</label>
<div class="col-sm-10">
<input type="text" class="form-control" ng-model="prenomet">
</div>
</div>
<div class="form-group">
<label>Email</label>
<div class="col-sm-10">
<input type="text" class="form-control" ng-model="email">
</div>
</div>
</div>
//click on this button will apply the modification
<div class="modal-footer">
<button id="button" data-dismiss="modal">OK</button>
</div>
</div>
</div>
//this button opens the modal in which I will modify the data
<button ng-click="open(store.Id)" data-target="#myModal">Modify</button>
</td>
</tr></tbody></table>
,这是控制器:
.controller("etudmodifCtrl", ["$scope", "$http", "logger","$route", function ($scope, $http, logger,$route) {
$scope.errors = [];
$scope.msgs = [];
$scope.open = function (Id) {
console.log("ID open---");
console.log(Id);
$http({method: 'GET', url: 'http://localhost:50001/api/Students/'+Id})
.success(function (data) {
$scope.errors.splice(0, $scope.errors.length); // remove all error messages
$scope.msgs.splice(0, $scope.msgs.length);
$scope.prenomet=data.FirstName;
$scope.nomet=data.LastName;
$scope.email=data.Email;
console.log("success display");
document.getElementById('button').onclick = function() {
console.log("ID après modifetud---");
console.log(Id);
$http({method: 'PUT',
url:'http://localhost:50001/api/Students/modifier/'+Id,
headers: {'Content-Type': 'application/json'},
data:'{"FirstName":"'+$scope.prenomet+'","LastName":"'+$scope.nomet+'","Email":"'+$scope.email+'"}'
}
)
.success(function() {
console.log("-----success-----"+Id);
console.log($scope.prenomet+" "+$scope.nomet+" "+$scope.email);
logger.logSuccess("etudiant a été modifié avec succès");
}).error(function() {
logger.logError("echec de modification de l'étudiant");
console.log("data error ...");
});
}
}).error(function (data, status, headers, config) {
console.log("data error ...");
});
}}])
每例如,如果我选择表的使用id = 6我得到正确的输入表单中的数据行,但当我尝试修改输入数据然后点击“OK”按钮时,我总是得到旧数据而不是修改后的数据
与 id = 6和$ scope.prenomet = Student6,$ scope.nomet = 6,$ scope.email = student6 @ yahoo.com(无修改),如果我将这些数据修改为scope.prenomet = student66,$ scope.nomet = 66,$ scope.email = student66 @ yahoo.com 我得到这个在控制台上:
ID open---
6
success display
ID après modifetud---
6
-----success-----6
Student6 6 [email protected]
success
你有什么想法,请有关问题,非常感谢帮助
感谢@buzzsaw代码,但是我在这一行有一个未定义的Id错误:url:'http:// localhost:50001/api/Students/modifier /'+ Id, – hanali
@hanali在你的$ scope .open()函数,添加行$ scope.rowId = Id,然后在editRow()函数的url字符串中用+ $ scope.rowId替换+ Id。这应该解决您的未定义的错误。 – buzzsaw
感谢buzzsaw它现在的作品:D – hanali