Search code examples
javascriptangularjsangular-filters

AngularJS filters dynamic apply depending on expression


I have snippet of a view. vm.items is an object, each item of object contains an email or phone and the code correspondingly. For example {value:'123456', code: 'phone'}.

.contacts
    .contact(ng-repeat='item in vm.items track by $index')
      span {{item | phone}}

The question is how could i apply filter only when item is a phone. It means the code of item is phone, and when it is an email leave it as is. I thought i could use ternary operator and have something like this: span {{item | item.code === "phone"? phone : '' }}, but i didn't succeed. I cannot change the phone filter code and pass any parameters to this filter. I cannot create new filter for email.


Solution

  • I would do following:

    {{ item.code === "phone" ? (item | phone) : item }}