无法通过ng-model获取隐藏字段值

时间:2017-12-04 15:04:11

标签: javascript html angularjs

我是Angular js的新手,我正在努力通过ng-model访问隐藏的字段值。基本上我正在使用Modal进行编辑。这是我的控制器代码:

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

我在下面的代码中将ng-model作为editId:

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

当我点击“提交”按钮时,我可以发布除id之外的其他值。建议我解决这个问题。提前感谢你!

2 个答案:

答案 0 :(得分:2)

简单的技巧...不是调用javascript来设置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">

答案 1 :(得分:1)

因为你使用js来设置editID的值,所以ng-model没有更新使用

$scope.editId = id;

更改输入值