Html PUT方法没有';nt在AngularJs工作
我被卡住了,因为我必须为我的工作创建一个更新方法,但是使用AngularJs表单没有获取ng模型信息。我不知道为什么。因为2小时前,我用一个简单的POST方法轻松地完成了 以下是我的控制器中的代码:Html PUT方法没有';nt在AngularJs工作,html,angularjs,spring,Html,Angularjs,Spring,我被卡住了,因为我必须为我的工作创建一个更新方法,但是使用AngularJs表单没有获取ng模型信息。我不知道为什么。因为2小时前,我用一个简单的POST方法轻松地完成了 以下是我的控制器中的代码: $scope.UpdateData = function (petId) { $http({ method: 'PUT', url: baseApiUrl + '/pets/' + petId, data: {
$scope.UpdateData = function (petId) {
$http({
method: 'PUT',
url: baseApiUrl + '/pets/' + petId,
data: {
id: petId,
name: $scope.updateName,
age: $scope.updateAge,
owner: $scope.updateOwner,
}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};
这是我的观点:
<table class="table table-hover table-bordered">
<thead>
<tr class="text-center pointer back-grey">
<th class="text-red">
Nom
</th>
<th class="text-red">
Age
</th>
<th class="text-red">
Propriétaire
</th>
<th class="text-red">
Action
</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="pet in pets">
<td><input type="text" placeholder="{{ pet.name }}" name="name" class="form-control" ng-model="updateName" required></td>
<td><input type="text" placeholder="{{ pet.age }}" name="age" class="form-control" ng-model="updateAge" required></td>
<td><input type="text" placeholder="{{ pet.owner }}" name="owner" class="form-control" ng-model="updateOwner" required></td>
<td><input id="update" type="submit" class="btn btn-danger disabled" ng-click="UpdateData(pet.id)" /></td>
</tr>
</tbody>
</table>
正如我所问的,这个id是给petId的,但是名字没有改变。当我用一个类似“hello”的字符串更改$scope.updateName时,这就起作用了
谢谢你的帮助 对所有对象“pet”使用相同的变量。因此,如果您有两个宠物,那么同一个变量'updateName'将有两个输入,这可能导致未定义的行为。 我建议你这样做:
<tr ng-repeat="pet in pets track by $index">
<td><input type="text" placeholder="{{ pet.name }}" name="name" class="form-control" ng-model="updateName[$index]" required></td>
<td><input type="text" placeholder="{{ pet.age }}" name="age" class="form-control" ng-model="updateAge[$index]" required></td>
<td><input type="text" placeholder="{{ pet.owner }}" name="owner" class="form-control" ng-model="updateOwner[$index]" required></td>
<td><input id="update" type="submit" class="btn btn-danger disabled" ng-click="UpdateData($index)" /></td>
</tr>
$scope.UpdateData = function (idx) {
$http({
method: 'PUT',
url: baseApiUrl + '/pets/' + petId,
data: {
id: $scope.pets[idx],
name: $scope.updateName[idx],
age: $scope.updateAge[idx],
owner: $scope.updateOwner[idx],
}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};
$scope.UpdateData=函数(idx){
$http({
方法:'放',
url:baseApiUrl+'/pets/'+petId,
数据:{
id:$scope.pets[idx],
名称:$scope.updateName[idx],
年龄:$scope.updateAge[idx],
所有者:$scope.updateOwner[idx],
}
}).then(函数成功回调(响应){
控制台日志(响应);
},函数errorCallback(响应){
控制台日志(响应);
});
};
我按照您的建议执行了操作,但当我尝试在字段中写入某些内容时仍然出现此错误:TypeError:无法设置未定义的属性“0”。您似乎没有初始化数组。您必须在范围初始化上执行此操作:$scope.updateAge=[];所有阵列也是如此;
<tr ng-repeat="pet in pets track by $index">
<td><input type="text" placeholder="{{ pet.name }}" name="name" class="form-control" ng-model="updateName[$index]" required></td>
<td><input type="text" placeholder="{{ pet.age }}" name="age" class="form-control" ng-model="updateAge[$index]" required></td>
<td><input type="text" placeholder="{{ pet.owner }}" name="owner" class="form-control" ng-model="updateOwner[$index]" required></td>
<td><input id="update" type="submit" class="btn btn-danger disabled" ng-click="UpdateData($index)" /></td>
</tr>
$scope.UpdateData = function (idx) {
$http({
method: 'PUT',
url: baseApiUrl + '/pets/' + petId,
data: {
id: $scope.pets[idx],
name: $scope.updateName[idx],
age: $scope.updateAge[idx],
owner: $scope.updateOwner[idx],
}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};