PUT方法在AngularJs中不起作用

问题描述:

Im被卡住了,因为我必须为我的工作创建一个Update方法,但是对于AngularJs,表单没有采用ng-model信息。我不知道为什么。因为在我使用简单的POST方法轻松完成之前2个小时。PUT方法在AngularJs中不起作用

这里是我的控制器代码:

$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> 

当我改变我的形式的值,然后按下按钮,我有这样的:

Object {data: "1", status: 200, config: Object, headers: function} 

该id是petId,因为我问,但名称不会改变。当我更改$ scope.updateName字符串像“你好”这是工作...

感谢您的帮助!

您对所有对象的“宠物”使用相同的变量。所以,如果你有两只宠物,同一个变量'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); 
    }); 
}; 
+0

我做了你的建议,但我仍然有这个错误,当我尝试写的领域里面的东西:类型错误:无法设置的未定义的属性“0”。 –

+0

好像你没有启动阵列。你必须在范围初始化上做到这一点:** $ scope.updateAge = []; **所有数组; – Vanderson