Reputation: 523
I define my functions outside the controller. I want to use $filter in one of the functions. You can see that it works inside the controller ($scope.test1 works) but it doesn't find $filter function in doSomething. Please help me figure it out. If i move doSomething inside the controller it will work but i dont want to do that.
angular.module('myapp',[])
.controller('appController',['$scope','$filter',function($scope,$filter) {
$scope.doSomething = doSomething;
//WORKS
$scope.test1 = $filter('filter')([{typeid:1},{typeid:2}], { typeid: 2 }, true);
}]);
function doSomething($filter) {
//DOES NOT WORK
test2 = $filter('filter')([{typeid:1},{typeid:2}], { typeid: 1 }, true);
alert(test2);
}
Upvotes: 0
Views: 126
Reputation: 31851
Try this
angular.module('myapp',[])
.controller('appController',['$scope','$filter',function($scope,$filter) {
$scope.doSomething = function() {
doSomething($scope, $filter);
};
}]);
function doSomething($scope,$filter) {
$scope.name = 'hello world';
$scope.result = $filter('filter')([{typeid:1},{typeid:2}], { typeid: 1 }, true);
alert($scope.result);
}
Upvotes: 0
Reputation: 201
$filter
works inside the controller because of dependency injection (when you declared the controller, you also specified its dependencies, such as $scope
and $filter
) while the doSomething function does not.
My questions would be:
1) Why declare doSomething
outside the controller? Is it for re-use?
2) Where do you intend to call doSomething
?
Upvotes: 2