0

Pretty new to angularjs, this is my struggle :

//my-filter.filter.js

function myFilter($filter){
    return filter(input){
        return $filter('currency')(input, 'X');;
    }
}

myFilter.$inject = ['$filter'];

export default myFilter;

And :

//my-component.component.js

class MyComponentController {
    constructor(currencyFilter){
        this.currencyFilter = currencyFilter;
    }

    $onChanges(changes) {
        this.message = changes.data.message;

        this.message = this.currencyFilter(this.message);
    }

    ...
}

And :

.filter('bsMyFilter', myFilter);

What I want to achieve is inside my-component.component.js not to use the default angular currencyFilter, but instead to use myFilter. Is this possible and how ? Thx

paulalexandru
  • 9,218
  • 7
  • 66
  • 94
  • I think you need to change your filter to: `app.filter('myFilter', function(){ ... });` then it will be automatically part of `$filter`, which is the only thing that you need to inject into your component: `constructor($filter){...` and then use it with `this.message = $filter('myFilter')(this.message);` (could be wrong somewhere with ES6 syntax, _adjust accordingly_) – Aleksey Solovey Dec 06 '17 at 11:44
  • I have something like .filter('bsMyFilter', myFilter); I don't know if I can change this, is there any other way to inject the filter ? How ? – paulalexandru Dec 06 '17 at 11:51
  • (I would avoid ES6 altogether) Here is everything about injections: [`$injector`](https://docs.angularjs.org/api/auto/service/$injector), and here is a possible injection for ES6 classes: [`$injector.annotate(fn)`](https://stackoverflow.com/questions/27529518/automatically-set-arguments-as-instance-properties-in-es6/37330930#37330930) – Aleksey Solovey Dec 06 '17 at 11:59
  • This is what I got, I have to work with this. Is there any solution to this ? – paulalexandru Dec 06 '17 at 12:06

1 Answers1

0

inject $filter, $filter('bsMyFilter') is you filter. At least in angular 1.6 - but I doupt they change it.

Note: if you not gonna use it in templates - just create factory.

Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38