MANOJ G Thank you. I have tried your approach and it'sh working for a basic scenario where thereis no dependencies required for your filter. If there are dependencies it fails. I guess it must require something similar to directive.
class TestFilter {
constructor(TestService) {
return (input, param1) => {
console.log('Filter Param: ', param1);
return input.slice(1);
}
}
}
TestFilter.$inject = ['TestService'];
export default angular.module('yourApp').filter('letterSlice', () => new TestFilter());
MANOJ G I'm using something like below for Directive and it seems like working, But not able to find a proper example for Filters
import template from './test.directive.html';
class ExampleDirective {
constructor($http) {
this.templateUrl = template; // Template or template Url
this.restrict = 'E'; // Element, Attribute, Class or Comment
this.scope = { // Bindings required
oneWay: '@',
twoWay: '=',
testFn: '&'
};
this.controllerAs = 'eCtrl';
this.bindToController = true; //This will bind data to controller's this but not to scope. So it's good to use it.
this.controller = ExampleController; //Controller Initialization
this.$http = $http; // Dependencies for Directive, if any required
}
link(scope, ele, attrs) { // Link function
console.log('LINK FUNCTION:', scope, ele, attrs);
}
static directiveFactory($http){ // This is what is exposed for directive
ExampleDirective.instance = new ExampleDirective($http);
return ExampleDirective.instance;
}
}
ExampleDirective.directiveFactory.$inject = ['$http']; // Inject Dependencies for Directive
class ExampleController {
constructor($scope, $http) {
console.log('DIRECTIVE CONTROLLER');
this.$scope = $scope;
this.$http = $http;
}
}
ExampleController.$inject = ['$scope', '$http']; // Inject Dependencies for Controller
export default angular.module('yourApp').directive('testDir', ExampleDirective.directiveFactory);
I have written a blog on a directive named "setHeight.ts" with class attribute using ES6 class.
Here I have used typescript.Please let me know if you have any more doubts.Writing filters is also the same approach like directive
Anand Reddy Rikka
A Continuous Learner and passionate coder
MANOJ G Found it. It's working !! Can you also check it once
class TestFilter { constructor($http, TestService) { this.$http = $http; this.TestService = TestService; return (input, param1) => { console.log($http); console.log(this.TestService.testFn()); console.log('Filter Param: ', param1); return input.slice(1); } } static filter($http, TestService){ TestFilter.instance = new TestFilter($http, TestService); return TestFilter.instance; } } TestFilter.filter.$inject = ['$http', 'TestService']; export default angular.module('yourApp').filter('letterSlice', TestFilter.filter);