Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of $parse in angular js [closed]

Tags:

Any ideas on where to use $parse of AngularJS.

Please give any examples or links which describes clearly.

like image 881
rinesh Avatar asked Jan 03 '14 05:01

rinesh


People also ask

What is the use of $Parse in AngularJS?

Angular runs $parse automatically when it runs the $digest loop, basically $parse is the way angular evaluates expressions. If you wanted to manually parse an expression, you can inject the $parse service into a controller and call the service to do the parsing for you.

What is the use of parse?

The Parse function enables the user to parse the data in one field in the source file and write the "parts" of the data to a field or fields in the target file. Parsing is done on a unique character, such as a space, an asterisk, a dash, etc.

What is parser in angular?

Parsers change how view values will be saved in the model and Formatters change how model values will appear in the view. Unfortunately, Angular has not implemented this feature in the new version, but we can achieve the same result with ControlValueAccessor .

What is $setValidity in AngularJS?

The $setValidity() function is a built-in AngularJS function that is used to assign a key/value combination that can be used to check the validity of a specific model value. The key in this case is “unique” and the value is either true or false.


2 Answers

Angular runs $parse automatically when it runs the $digest loop, basically $parse is the way angular evaluates expressions. If you wanted to manually parse an expression, you can inject the $parse service into a controller and call the service to do the parsing for you.

Here's a code snipped from ng-book that watches then parses an expression.

<div ng-controller="MyCtrl">   <input ng-model="expr" type="text" placeholder="Enter an expression" />     <h2>{{ parsedValue }}</h2> </div> 

then in our module,

angular.module("myApp", [])  .controller('MyCtrl',['$scope', '$parse', function($scope, $parse) {     $scope.$watch('expr', function(newVal, oldVal, scope) {       if (newVal !== oldVal) {         // Let's set up our parseFun with the expression         var parseFun = $parse(newVal);         // Get the value of the parsed expression          $scope.parsedValue = parseFun(scope);       }     });  }]); 
like image 64
Tyler McGinnis Avatar answered Sep 30 '22 21:09

Tyler McGinnis


you likely won't use $parse directly, but it is what converts angular expressions into JavaScript functions. Expressions are JavaScript-like code snippets that are usually placed in bindings such as {{ expression }}.

like image 34
Claies Avatar answered Sep 30 '22 19:09

Claies