1) When we create custom filter then is it mandatory to inject the custom filter in controller to call it from controller function?
I have seen one code where person create custom filter and call it from controller function...is it possible? Just see the code. I have not tested it.
<div ng-app="myapp" ng-controller="myctl">
<input id="btnok" type="button" value="ShowItems" ng-click="myfunction();" />
{{result}}
</div>
var app = angular.module("myapp", []);
app.filter("CustomFilter", function () {
return function (input) {
var out = [];
for (var i = input.length - 1; i >= 0 ; i--) {
out.push(input[i]);
}
return out;
}
});
app.controller("myctl", function ($scope, $filter) {
$scope.Items = ["C#", "JavaScript", "Html"];
$scope.myfunction = function () {
$scope.result = $filter('CustomFilter')($scope.Items);
}
})
Just see that custom filter called CustomFilter
has not been injected in controller but filter is calling from controller function.....is it right?
2) Is it possible to use different names when injecting custom filter in controller? See one code and it is taken from this url http://budiirawan.com/angular-js-use-filter-controller/
(function(angular) {
'use strict';
angular.module('appModule')
.filter('hello', function() {
return function(input) {
return 'hello ' + input;
}
});
})(window.angular);
(function(angular) {
'use strict';
angular.module('appModule', [])
.controller('appController', ['$scope', '$filter', 'helloFilter', function($scope, $filter, helloFilter) {
$scope.name1 = $filter('hello')('budi');
$scope.name2 = helloFilter('sinta');
}]);
})(window.angular);
See hello filter has been injected in controller with a different name called helloFilter how can it work......How angular resolve or understand what we are trying to point out hello filter ? Please guide me.
Thanks
1) You don't have to inject the custom filter into the controller you could inject $filter
into the controller instead of adding your custom filters.
2) Angular adds Filter after filters when doing dependency injection from the examples I've seen online (not 100% sure why I always inject $filter into my controllers rather then the filter itself)
http://sravi-kiran.blogspot.co.uk/2013/12/InvokingAngularJsFiltersFromController.html http://budiirawan.com/angular-js-use-filter-controller/ https://dzone.com/articles/invoking-angular-js-filters