我要注入$ HTTP在我的自定义过滤器,使用它在我的观点:注入dependecy自定义过滤器angularJS
angular.module('demo').filter('quesByID',['$http', function() {
var input="";
return function(input) {
$http.get("http://localhost:3003/api/quiz/"+input).then(function(reponse){
input=reponse.data;
});
return input;
}
}]);
我有错误$ HTTP是没有定义
angular.js:13920 ReferenceError: $http is not defined
at getQuestionById.js:4
at fn (eval at compile (angular.js:14817), <anonymous>:4:198)
at regularInterceptedExpression (angular.js:16043)
at expressionInputWatch (angular.js:15948)
at Scope.$digest (angular.js:17515)
at Scope.$apply (angular.js:17790)
at done (angular.js:11831)
at completeRequest (angular.js:12033)
at XMLHttpRequest.requestLoaded (angular.js:11966)
在哪里我应该注入“$ http”的依赖吗?
实际上你需要提供'$ http'作为该功能的参数;即'函数($ http){' – Claies
为什么你使用这个过滤器? – charlietfl
您正在滥用筛选器。改为使用服务。在控制器中这样做(如答案所示)是一种快速解决方法,但很难称之为最佳实践。 – estus