Ng模型无法访问

时间:2016-06-17 12:06:41

标签: angularjs

其中一个ng模型给出了null值。当我单击具有函数setNewRecord的按钮时,参数selectedDocument为空。前两个参数是正确的。还添加了javascript代码。

<form name="myForm" >
<div>
    <select ng-model="selectedCompany">
        <option value="">-- Select Company --</option>
        <option data-ng-repeat="currentSetting in currentSettings" value={{currentSetting.SCACCode}}>{{currentSetting.SCACCode}}</option>
    </select>
</div>
<div><input id="Text1" type="text"  ng-model="enteredCustomer"/></div>
<div>
   <select ng-model="selectedDocument" ng-click="getTypes(selectedCompany, enteredCustomer)">
       <option value="">-- Select Doc type --</option>
       <option data-ng-repeat="docSetting in docSettings" value="{{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option>
   </select>
</div>
<input id ="btnAdd" type="button" value="Add new record" ng-click="setNewRecord(selectedCompany, enteredCustomer,selectedDocument)"/>

Java脚本

myApp.service('getDocTypesService', ['$http', '$q', function($http, $q) {
var allSettings = null;
this.getDocTypes = function(compName, custName) {
    var def = $q.defer()
    if (allSettings) {
        def.resolve(allSettings);
    } else {
        $http.post('GetDocTypes', {
                companyName: compName,
                customerName: custName
            })
            .then(function(response) {
                var response = $.parseJSON(response.data)
                allSettings = response;
                def.resolve(allSettings);
            });
    }
    return def.promise;
}
}]);



myApp.controller('myController', ['$scope', 'getDocTypesService',
function($scope, getDocTypesService) {
    $scope.getTypes = function(comp, cust) {
        getDocTypesService.getDocTypes(comp, cust).then(function(value) {
            $scope.docSettings = value
        });
    };

}
]);

1 个答案:

答案 0 :(得分:0)

尝试用以下方法替换selectedDocument的选择:

<select ng-model="selectedDocument" 
        ng-click="getTypes(selectedCompany, enteredCustomer)" 
        ng-options="docSetting.Doc_Type for docSetting in docSettings track by docSetting.Doc_Type">
</select>