Angular custom filter, angularfilter

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.