Angular Modal Form返回加倍的JSON值

时间:2015-04-23 21:36:25

标签: javascript jquery json angularjs twitter-bootstrap

所以我有一个工作模态,我试图取代updateUser.pubData.firstName和updateUser.pubData.lastName值,但是当我改变值并单击OK,以下对象被返回到控制器(pubData值重复,而不是替换。)

有什么想法我需要做什么来替换pubData中的firstName和lastName值而不是重复它们?谢谢!

{_id:' 55394b4e87fd7088751c4809'  用户名:' d',  密码:' x',  __v:0,  pubComments:[],  pubData:   {firstName:' dan',     lastName:' darby',     名字:' dave',     姓氏:' deefe' },  书架:[],  角色:[' admin','学生' ]}

控制器模态处理程序:

     $scope.openUpdateUserModal = function (user) {
console.log(user.pubData.firstName);
console.log(user);
$scope.updateUser = user;
//$scope.firstname = user.pubData.firstName;
//$scope.lastname = user.pubData.lastName;
$("#openUpdateUserModal").modal('show')};

  $scope.updateUserInDb = function (updatedUser) {
console.log("updateUserInDb");
console.log(updatedUser);
$http.post("/api/updateUser", updatedUser)
   .success(function (resource) {
     $scope.users = resource;
     $("#openUpdateUserModal").modal('hide')
   });};

模态HTML来电按钮

        <button class="btn btn-warning pull-right" ng-click="openUpdateUserModal(user)">Update</button>

模态表格

        <div class="modal fade" id="openUpdateUserModal" tabindex="-1" role="dialog" aria-labelledby="courseModalLabel" aria-hidden="true">
        <div class="modal-dialog">
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">Edit User</h4>
  </div>
  <div class="modal-body">
    <form class="form form-horizontal" name="userUpdateForm" id="userUpdate-form"><div class="form-group">
        <label class="col-sm-3 control-label" for="name">First Name</label>
        <div class="col-sm-8">
          <input ng-model="updateUser.pubData.firstname" id="name" name="name" type="text" class="form-control" placeholder="{{updateUser.pubData.firstName}}" />
        </div>
      </div>

      <div class="form-group">
      <label class="col-sm-3 control-label" for="name">Last Name</label>
      <div class="col-sm-8">
        <input ng-model="updateUser.pubData.lastname" id="name" name="name" type="text" class="form-control" placeholder="{{updateUser.pubData.lastName}}" />
      </div>
  </div>
    </form>
</div>
  <div class="modal-footer">
    <button ng-click="updateUserInDb(updateUser)" type="button" class="btn btn-primary">Ok</button>
    <button type="button" data-dismiss="modal" class="btn btn-default">Cancel</button>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

Javascript属性/字段名称区分大小写。 在您的标记中,您将ng-model属性指向updateUser.pubData.lastname,而我认为正确的属性为.lastName

赠品为firstName: 'dan', lastName: 'darby', firstname: 'dave', lastname: 'deefe' - 注意大写与小写&#39; n&#39;