Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  70] [ 2]  / answers: 1 / hits: 39571  / 11 Years ago, fri, january 3, 2014, 12:00:00

Any ideas on where to use $parse of AngularJS.



Please give any examples or links which describes clearly.


More From » angularjs

 Answers
17

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);
}
});
}]);

[#73409] Thursday, January 2, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nikokorbinm

Total Points: 744
Total Questions: 126
Total Answers: 104

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
;