[AngularJS] Using $parse Service

Source: Internet
Author: User

$parse is useful if you want to parse an expression and the context are not defined yet.

For example, I has a table component which allows user to pass in all the row items and define action for each row.

    <ttmd-tableItems= "Vm.invoices"Headers= "Vm.headers"    >        <ttmd-actions>            <ttmd-actionif= "Shouldpay"text= "Pay"On-click= "Vm.pay (payload)"            ></ttmd-action>        </ttmd-actions>    </ttmd-table>

What I want was only if ' Shouldpay ' was true when display of the ' ttmd-action ', if not just hide it, so there was "if" attr in T He tag.

But there are one problem to make it works:because ' Shouldpay ' prop locates on each item, if we use ng-repeat, we would do Somthing like this:

<ng-repeat= "Item in Items $index">    <  Ttmd-action         if= "Item.shouldpay"    ></ttmd-action > </ Div    

But I don ' t want-component hard for user-use, so the problem we need to solve here's

    • Parse the expression we passed in with the right context

So are $parse come into play:

    Shoulddisplay () {        this. $parse.  If//  get the expression and conver it to a functionthis        //  Find the RI Ght context        console.log (context);         Console.log (getter (context));         return // parse the expressionwith the right context    }

[AngularJS] Using $parse Service

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.