2017-12-04 13 views
2

Angular jsの新機能で、ng-modelを介して非表示のフィールド値にアクセスするのに苦労しています.Basically私は編集のためにModalを使用しています。コントローラのためのコード:ng-model経由で値を取得できません

$scope.update = function(){ 
      $http({ 
       method:'POST', 
       url:'http://localhost:8000/api/v1/edit', 
       data: {id: $scope.editId , name: $scope.edit_name, email: $scope.edit_email, password: $scope.edit_password} 
      }).then(function(response){ 
       $scope.updated_form_data_list = response.data; 
      }); 
     }; 
    }); 

私はjavascriptを介して、各レコードのIDを設定しようとしている:

<tbody ng-repeat="form_data in form_data_list"> //Displaying records from DB inside the Table 
       <tr> 
       <td>@{{ form_data.name }}</td> 
       <td>@{{ form_data.email }}</td> 
       <td>@{{ form_data.password }}</td> 
       <td><Button type="button" onclick="setEditId(this.id)" data-toggle="modal" data-target="#modal_content" id="@{{form_data.id}}" ><span class="glyphicon glyphicon-pencil"></span></Button></td> 
       </tr> 
    </tbody> 

そして、私はjavascriptを介して、隠されたために値を設定して、ここにそれが行く:

<script type="text/javascript"> //setting the value of hidden field with id 
    function setEditId(id){ 
     document.getElementById('editId').value=id; 
    } 
</script> 

私は以下のコードでeditIdとしてモデルngをしている:私は、ID以外の他の値を投稿することができるよSubmitボタンをクリックすると

<div class="modal fade" id="modal_content" role="dialog"> 
    ... 
    <input type="hidden" name="id" id="editId" ng-model="editId"> 
    ... 
<button ng-click="update()">Submit</button> 

。このための解決策を私に提案してください。よろしくお願いします!

答えて

2

簡単なトリックの値を変更するために使用

$scope.editId = id; 

が更新されませんId、ng-clickを使ってコントローラーで設定します。

$scope.storeId=function(id){ 
       $scope.id=id; 
} 

そして

<Button type="button" ng-click="storeId(form_data.id)" data-toggle="modal" data-target="#modal_content"> 
<span class="glyphicon glyphicon-pencil"> 
+0

経由で呼び出され、本当に便利ではkeerthivasanありがとう... – BharathRao

1

あなたはeditIDのNG-モデルの値を設定するJSを使用しているので、代わりに設定するためのJavaScriptを呼び出すのでは...あなたの入力

関連する問題