2016-03-31 73 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