$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