任何关于在哪里使用AngularJS的$parse
的想法。
请提供任何例子或链接,清楚地描述。
发布于 2014-01-03 13:44:24
Angular在运行$digest循环时会自动运行$parse,基本上$parse是angular计算表达式的方式。如果您希望手动解析表达式,可以将$parse服务注入控制器并调用该服务来为您执行解析。
下面是从ng-book中截取的代码,它监视然后解析一个表达式。
<div ng-controller="MyCtrl">
<input ng-model="expr" type="text" placeholder="Enter an expression" />
<h2>{{ parsedValue }}</h2>
</div>
然后在我们的模块中,
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);
}
});
}]);
发布于 2014-01-03 13:21:50
您可能不会直接使用$parse,但正是它将angular expressions转换为JavaScript函数。表达式是类似JavaScript的代码片段,通常放在{{ expression }}
等绑定中。
https://stackoverflow.com/questions/20896870
复制相似问题