Tyl*_*nis 25
当运行$ digest循环时,Angular会自动运行$ parse,基本上$ parse是angular计算表达式的方式.如果要手动解析表达式,可以将$ parse服务注入控制器并调用服务为您执行解析.
这是从ng-book剪下的代码,然后观察表达式.
<div ng-controller="MyCtrl">
<input ng-model="expr" type="text" placeholder="Enter an expression" />
<h2>{{ parsedValue }}</h2>
</div>
Run Code Online (Sandbox Code Playgroud)
然后在我们的模块中,
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);
}
});
}]);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
34167 次 |
最近记录: |