javascript - $filter 不是函数 AngularJS

标签 javascript angularjs angularjs-scope angularjs-filter angularjs-http

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/

相关文章:

angularjs - 在有效模糊上调用函数

javascript - $scope 不从表单输入中获取字符串 - AngularJS,JS

javascript - 开发一个将保存在本地的设置页面

javascript - 从自定义 React 组件库导入和使用组件会导致不变违规 : Invalid hook call

javascript - Angularjs 中的 localStorage 在选项卡之间共享

angularjs - ng-init 是否像 ng-model 一样监视实例化属性的变化?

javascript - "Cannot read property ' childNodes ' of undefined"使用 list.js 将 Controller 添加到列表元素

javascript - Angularjs 别名作用域

javascript - 如何使用javascript比较日期字符串

html - 为什么我的日历图标没有出现在表格单元格中?