admin管理员组文章数量:1187345
I am trying to inject a filter into my controller and use it as such:
angular
.module('graduateCalculator', [])
.filter('slug', function() {
return function(input) {
if(input) {
return input.toLowerCase().replace(/[^a-z-]/g, '-');
}
};
}).controller('GraduateCalculatorController', ['$filter', app.graduateCalculator($filter)]);
However, I get the above error. I am obviously doing something very wrong - just not obvious enough to me.
Uncaught ReferenceError: $filter is not defined
It may have something to do with my function. It's written like so:
var app = {
graduateCalculator: function($filter) {
window.console.log( $filter('slug')('A random looking string...') );
}
};
Help appreciated!
I am trying to inject a filter into my controller and use it as such:
angular
.module('graduateCalculator', [])
.filter('slug', function() {
return function(input) {
if(input) {
return input.toLowerCase().replace(/[^a-z-]/g, '-');
}
};
}).controller('GraduateCalculatorController', ['$filter', app.graduateCalculator($filter)]);
However, I get the above error. I am obviously doing something very wrong - just not obvious enough to me.
Uncaught ReferenceError: $filter is not defined
It may have something to do with my function. It's written like so:
var app = {
graduateCalculator: function($filter) {
window.console.log( $filter('slug')('A random looking string...') );
}
};
Help appreciated!
Share Improve this question edited Aug 13, 2015 at 11:35 Michael Giovanni Pumo asked Aug 13, 2015 at 11:22 Michael Giovanni PumoMichael Giovanni Pumo 14.8k18 gold badges99 silver badges145 bronze badges 04 Answers
Reset to default 16The 2nd parameter to [module].controller
is a constructor function, not calling the constructor function...
// change this...
.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator($filter)]);
// to this...
.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator]);
You controller function (for which you currently have a function call, instead of a callback function) needs to be a function which takes $scope (controllers require $scope), also containing $filter.
controller('GraduateCalculatorController', ['$scope', '$filter', function($scope, $filter){<your code here>}]);
There are, however, two ways to use your filter. You can either inject $filter and call
$filter('slug')(inputStuff)
or you can inject slugFilter and just call it directly
slugFilter(inputStuff)
One of the most common complaints about angular is the error messages, and this seems to be the problem here!
If I wasn't clear enough on anything let me know and I'll do my best to help.
I think error happen when you try to define a controller and inject $filter
into your app.graduateCalculator($filter)
.
With this way you invoke the function app.graduateCalculator($filter)
. But the js interpreter didn't know what is $filter
, so it threw the exception.
Just update your controller into this,
.controller('GraduateCalculatorController', ['$filter', app.graduateCalculator]);
And make sure your graduateCalculator
have the parameter $filter
.
you should wrap your contorller with a function
angular.module('graduateCalculator', [])
.filter('slug', function() {
return function(input) {
if(input) {
return input.toLowerCase().replace(/[^a-z-]/g, '-');
}
};
})
.controller('GraduateCalculatorController', ['$filter', function($filter) {
app.graduateCalculator($filter)
}]);
本文标签: javascriptAngularJSfilter is not definedStack Overflow
版权声明:本文标题:javascript - AngularJS - $filter is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738300971a2073604.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论