2016-03-31 74 views
0

这里保存ng模型是将newattendance保存到数据库。 “newattendance._id” 不作为一个ng-model.how使它 “newattendance._id” 是NG-模型在保存数据时ng模型数据没有得到

<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("[email protected] " + _id) 
     attendanceDetails = attendanceResource.query({"id": _id}); 
     return attendanceDetails; 
    }, 


} 
}) 

请帮助我如何让它为NG-模型,以及如何拯救这个...

+1

请添加完整的代码。 –

回答

0

我为您创建了一个JSFiddle,希望能够帮助您理解角度中的2路绑定。

您不需要将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); 
    } 

}); 
+0

感谢您的回复。我知道这两种方式的约束力。我的问题是ng-model的ng-show无法正常工作。我从选择选项中获取数据,并使用ng-show将数据绑定到输入框。但ng-model不具有约束力newattendance._id – SrinivasAppQube

+0

请提供响应json,我将根据api newattendance随意更新我的答案 –

+0

._id必须使用ng-model发送。保存时检查工厂。回应是404错误。我发送newattendance._id作为手动工作。 – SrinivasAppQube