About Angularjs's $filter (rookie on the road)

Source: Internet
Author: User

Now we're going to use Ng's $filter service to filter the data

For example, now the data returned to us in some background is this:

$scope. Data ={ID:" on", Maxdistkts:0, Name:"Common Area", Seats: [], tickets:[{price: -, Name:"Volkswagen single Ticket", Fee:NULL, FAVORABLEFLG:"N", Maxtkts:0, MARKETPRICEFLG:"N", Isdiscount:NULL}, {price: -, Name:"long-card double-person ticket", Fee:NULL, FAVORABLEFLG:"N", Maxtkts:0, MARKETPRICEFLG:"N", Isdiscount:NULL}, {price: -, Name:"Watercress", Fee:NULL, FAVORABLEFLG:"N", Maxtkts:0, MARKETPRICEFLG:"N", Isdiscount:NULL}, {price: -, Name:"Adult Ticket", Fee:NULL, FAVORABLEFLG:"N", Maxtkts:0, MARKETPRICEFLG:"Y", Isdiscount:NULL                    }                ]            };

If we are going to show the name and price of the various tickets on the page, the "VW single ticket" in the data is not what we need, so we have to filter it.

 $scope. fn = function   () { for  (var  i=0; i< $scope. Data.tickets.length;i++ if  ($scope. Data.tickets[i].name = ' VW single ticket '  = $scope. Data.tickets[i].name; }               }
              $scope. Parent.low= $filter (' filter ') ($scope. data.tickets,{' name '
Console.log ("========="
Console.log ($scope. Parent.low

$scope. Fn ();

See the data under the Chorme console

In fact, the data obtained now is the same as defined below (the following is a more intuitive point).

$scope. Parent ={Low:""}; $scope. Parent.low={price: -, Name:"Volkswagen single Ticket", Fee:NULL, FAVORABLEFLG:"N", Maxtkts:0, MARKETPRICEFLG:"N", Isdiscount:NULL                };

Now we can filter on this condition and get our data.

The first way
$scope. F = function(e) {               //return e.name! = "Adult ticket";                return e.name ! = $scope. parent.low.name;            }; 
= $filter (' filter '= $filter (' filter ') ($scope. Data.tickets,function(e) {return E.name ! = $scope. Parent.low.name;})

Console.log ($scope. Datafilter);

In fact, our first way is the same as the second one, but the way we behave is different.

The following data can be found on Google's console

So we get the data we need, and then we repeat it out on the page.

<div ng-repeat= ' t in Datafilter ' >    <p>{{t.name}}</p>    <p ng-bind= "T.price" ></p ></div>

In fact, the second way can be deformed, such as the following

function (e) {               //  return e.name! = "Adult ticket";                return e.name ! = $scope. parent.low.name;            }; <div ng-repeat= ' t in datafilter on HTML page | filter:{' name ': F} ' >    <p>{{t.name}}</p>    <p ng-bind= ' T.price ' ></p></div>

Smart little Friends will say, you can try to filter directly on the page, instead of writing JS

But I only found the data that I can only filter out the conditions I give, and I can't filter out the data that gives the condition, for example

<div ng-repeat= "T in Data.tickets | filter{' name ':p arent.low.name} ">  // or <div ng-repeat=" T in Data.tickets | filter:t.name = Parent.low.name ">    <p>{{t.name}}</p>    <p ng-bind=" T.price "></p></ Div>

This will filter out the data that we don't want and display it on the page. (the "VW single ticket" will be shown above)

Has the wood has the clever small companion to tell me how to filter out the data I need in the page, but does not have to write JS?

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.