模型不会改变angularjs

时间:2018-01-11 22:14:02

标签: angularjs

我在表格中有客户列表。 当我点击editClient时,客户端的名称将显示在输入文本中。这完美无缺。

 <body ng-app="myClientApp" ng-controller="myClientController">
        <a class="btn btn-success" ng-click="chargerClients()">
               Charger Clients
        </a>                        
        <div ng-if="clients">
             <table>
                <tr ng-repeat="cl in clients >
                <td>{{cl.idClient}} </td>
                <td>{{cl.nomClient}} </td>

                 <td>   <a class="btn btn-success" ng-click="editClient(cl.idClient,cl.nomClient)"></td>
              </table>
      <div>
                <input type="text" ng-model="id">
                <input type="text" ng-model="nom">
                <button ng-click="modifyClient()"> validate</button>
       </div>
    </div>
</body> 

问题是当我点击modifyClient时,它保持相同的值并且没有任何反应。我该如何解决这个问题?

<script>
var app=angular.module("myClientApp", []);
app.controller("myClientController", function($scope,$http,$window) {

$scope.chargerClients=function(){

        $http.get("/clients")
        .success(function(data){
            $scope.clients=data;

        });
    };

    //load id and name 
$scope.editClient=function(idClient, nomClient){
$scope.id=idClient; 
$scope.nom=nomClient;}

//modifyClient

$scope.modifyClient=function(){
        var params="";
        var params = ({
                idClient:$scope.id,
                nomClient: $scope.nom});
$http({
            method: 'PUT',
            url: "clients",
            data: params,
            headers: {'Content-type': 'application/json'}

        })
        .success(function(data) {
            $window.alert($scope.id+$scope.nom);
            $scope.chargerClients();
            $window.location.href = '/index.html'

        })
};

}
</script>

2 个答案:

答案 0 :(得分:0)

尝试在modifyClient中使用POST方法

答案 1 :(得分:0)

谢谢你我解决了;我必须在ng-model中添加链接:

`<ng-model="client.id">` 

并在剧本中:

$scope.client={}
$scope.editClient=function(idClient, nomClient){
$scope.client.id=idClient;
} 

$scope.modifyClient=function(){ 
var params=""; 
var params = ({ 
idClient:$scope.client.id 
});