保存数据时无法获取ng-model数据

时间:2016-03-31 08:49:59

标签: angularjs

这里保存ng-model是新的数据库保存。 “newattendance._id”不作为ng-model.how使其成为“newattendance._id”是ng-model

 <select class="form-control" ng-options="item.empcode as item.empcode for item in totemplist" ng-model="item.empcode">
                </select>
<input type="text" ng-repeat="newattendance in totemplist" ng-model="newattendance._id" ng-show="item.empcode ==newattendance.empcode" style="width:200px;" ><br>
<input placeholder="Enter Attendacne Date" ng-model="newattendance.doa">

<button class="btn btn-primary" ng-click="checkOut()">checkOut</button>

控制器

EmpMasterController.controller("AttendanceController", ['$scope', 'AttendanceFactory',"EmpAddService", function($scope, AttendanceFactory,EmpAddService){
$scope.newattendance={};
$scope.totemplist=EmpAddService.getAllEmpAddItems();
console.log($scope.totemplist);
$scope.checkIn = function(){
    AttendanceFactory.addAttendance($scope.newattendance);
    $scope.newattendance = {}
}   
$scope.getAllAttendance = function(){

     console.log("$$$$$"+$scope.newattendance._id)
     $scope.attendancedetails =AttendanceFactory.getAllAttendance($scope.newattendance._id);

}

}])

EmpFactModule.factory("AttendanceFactory", function($resource, RES_URL){
var attendanceResource = $resource(RES_URL+"attandence/:id/:attid", 
                    {"id": "@id", "attid": "@attid"}, {update: {method: "PUT"}})
var attendanceDetails;
return {
    addAttendance: function(newattendance){
        console.log("..1.. " + newattendance._id)
        attendanceResource.save({"id":newattendance._id}, newattendance, function(data){
            console.log("Add Success ")
        }, function(data, status){
            console.log("Add Failed*****");
        })
    },
    getAllAttendance: function(_id){
        console.log("..@.. " + _id)
        attendanceDetails = attendanceResource.query({"id": _id});
        return attendanceDetails;
    },


}
})

请帮助我如何将其作为ng-model以及如何保存...

1 个答案:

答案 0 :(得分:0)

我为你创建了一个JSFiddle,希望能帮助你理解角度的双向绑定。

您不需要将newattendance对象传递给结帐功能,它已保存在scope上。

HTML:

<div ng-app="app">
  <div ng-controller="formValidation">
    <div>
      <div>
        <span>User Name</span>
        <input type="text" placeholder="John" ng-model="newattendance._id">
        <span>
          <button ng-click="submit()">
            check out
          </button>
        </span>
      </div>
    </div>

    <pre>{{newattendance._id}}</pre>

  </div>
</div>

JS:

var app = angular.module('app', []);

app.controller('formValidation', function($scope) {

  $scope.submit=function(){
    var formPost = {
      "Username":$scope.newattendance._id
    };
    console.log(formPost);
    }

});
相关问题