我一直在网上搜索,但我找不到为什么会收到此错误
从控制器调用服务功能时,在控制台中" checkDishService.checkDish不是函数"
。
请帮忙!
这是服务:
(function () {
'use strict';
angular.module('public')
.service('checkDishService', checkDishService);
checkDishService.$inject = ['$http','$q', '$timeout']
function checkDishService($http,$q, $timeout) {
var service = this;
var items = [];
service.checkDish = function (shortName) {
var deferred = $q.defer();
$http.get('https://www.example.com/menu_items/'+shortName+'.json').success(function(data) {
service.items = data;
// Wait 2 seconds before returning
$timeout(function () {
deferred.resolve(data);
}, 400);
})
.error(function() {
deferred.reject("No such dish exists");
});
return deferred.promise;
};
}
})();
这是控制器:
(function () {
"use strict";
angular.module('restaurant', ['public'])
.controller('RegistrationController', RegistrationController);
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController($stateParams, checkDishService, $scope) {
var reg = this;
reg.checkDish = function(shortName){
if(shortName)
{
checkDishService.checkDish(shortName).then(function(res){
//the line before is the line throwing the error
});
}
}
}
})();
以下是我打电话给控制器的表格:
<div ng-controller="RegistrationController as reg">
<form name='regForm' novalidate>
<input type="text" name="dish" placeholder="Favorite dish"
ng-model="reg.user.dish" ng-blur="reg.checkDish(reg.user.dish)"
required
minlength="1"
maxlength="4">
{{ reg.user.dish }}
<br>
<span ng-show="regForm.dish.$invalid.checkDish">No such dish exists.</span>
<button
ng-disabled="regForm.$invalid"
ng-click="reg.submit()">Submit</button>
</form>
</div>
checkdish功能试图在用户点击提交按钮之前检查菜是否存在。
非常感谢
答案 0 :(得分:0)
您必须以正确的注射顺序传递参数。像这样更改订单
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams) {
答案 1 :(得分:0)
参数顺序应始终与注射顺序相同。
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams )
无论如何,我希望你得到答案。