Angular custom filter, angularfilter
Angular custom filter
In addition to several commonly used filters, angular can also customize filters to meet different requirements. It briefly studies custom filters and records them.
In the following scenarios, status may be an English string in the data returned in the background. if you use if in html for one-on-one judgment, it may seem a bit cool. In this way, we can implement it using a custom filter.
Javasc code:
Var myapp = angular. module ('demoapp ', []); myapp. controller ('filtercontroller', ['$ scope', function ($ scope) {$ scope. _ status = ['enabled', 'Disabled ', 'binding'] ;}]); myapp. filter ('statusfilter', function () {return function (input) {if (input = 'enabled') return 'enabled '; if (input = 'Disabled ') return 'stopped'; if (input = 'binding') return 'bine ';}});
Html code:
<p ng-repeat="status in _status" >{{status | statusFilter}}</p>
In the controller, we define an array _ status to simulate the data returned from the background. We may not want to directly display the data in English at the front-end. The customer cannot understand it... If ng-if is used in html for judgment and then display, it will look a little cool. We can directly write a simple filter to filter the status field.