Angularjs 从控制器调用服务函数
Angularjs calling a service function from the controller
我一直在网上搜索,但找不到出现此错误的原因
"checkDishService.checkDish is not a function"
从我的控制器调用服务函数时在控制台中。
请帮忙!
这是服务:
(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 函数试图在用户单击提交按钮之前检查菜品是否存在。
非常感谢
您必须按照正确的注入顺序传递参数。像这样更改顺序
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams) {
参数顺序应始终与注入顺序相同。
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams )
无论如何,我希望你能得到答案。
我一直在网上搜索,但找不到出现此错误的原因
"checkDishService.checkDish is not a function"
从我的控制器调用服务函数时在控制台中。
请帮忙!
这是服务:
(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 函数试图在用户单击提交按钮之前检查菜品是否存在。
非常感谢
您必须按照正确的注入顺序传递参数。像这样更改顺序
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams) {
参数顺序应始终与注入顺序相同。
RegistrationController.$inject = ['checkDishService','$scope','$stateParams'];
function RegistrationController(checkDishService, $scope, $stateParams )
无论如何,我希望你能得到答案。