app.controller('myController', ['$scope', '$http', '$filter', function($scope, $http, $filter) {
以上是我的代码示例,其中我尝试在我的 Controller
中使用$http.get
和$filter
。
唯一的问题是,当我这样使用它时,控制台日志会抛出一个错误,提示 $filter is not a function
。
app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {
当我交换它们时会抛出一个错误,提示 $http is undefined
最佳答案
当你使用
app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {
变量$filter
实际上是$http
的实例,而$http
是$filter
的实例.实际上,在 function(...)
params 中写什么并不重要。
这里重要的是您使用的注入(inject)剂的顺序,例如
app.controller('myController', ['$scope', '$http', '$filter', function(a, b, c) {
将映射到实例:
- 一个 -> 范围
- b -> $http
- c -> $过滤器
来自 Angular 文档:
Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.
因此,通过为您的 Controller 使用数组表示法,您可以确保代码在压缩后仍能正常工作。
关于javascript - $filter 不是函数 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24284655/