我正在尝试将两个变量从 Angular 服务发送到 mvc Controller 。我总是遇到错误。现在我收到 compName 不存在的错误。我已经堆积了几个小时,我尝试使用 Angular 调试器对其进行调试,但到目前为止还没有成功。 我认为我的错误与 Angular Controller 试图在加载时调用服务而不是在单击时调用服务有关,但我不知道如何解决这个问题。
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;
}
}]);
这是我的 Angular Controller 和服务:
myApp.controller('myController', ['$scope', 'getDocTypesService',
function ($scope, getDocTypesService) {
$scope.docSettings = '';
getDocTypesService.getDocTypes(compName, custName).then(function (value) {
$scope.docSettings = value
})
};
}
]);
这是 HTML:
<select ng-model = "selectedDocument" ng-click="getDocTypes(selectedCompany, enteredCustomer)">
<option>Select document</option>
<option ng-repeat="docSetting in docSettings" value=" {{docSetting.Doc_Type}}">{{docSetting.Doc_Type}}</option>
</select>
最佳答案
试试这个:
Controller :
$scope.getTypes = function(comp, cust) {
getDocTypesService.getDocTypes(comp, cust).then(function (value) {
$scope.docSettings = value
});
};
模板:
<select ng-model = "selectedDocument" ng-click="getTypes(selectedCompany, enteredCustomer)">
观察:selectedCompany 和 EnteredCustomer 必须是在点击发生之前定义的 $scope 变量。
关于javascript - Angular 服务参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37879035/