gpt4 book ai didi

javascript - 在 Angular JS 中使用 $parse

转载 作者:行者123 更新时间:2023-12-02 22:43:28 25 4
gpt4 key购买 nike

关于在哪里使用的任何想法 $parse AngularJS

请给出任何清楚描述的示例或链接。

最佳答案

Angular 在运行 $digest 循环时自动运行 $parse,基本上 $parse 是 Angular 计算表达式的方式。如果您想手动解析表达式,可以将 $parse 服务注入(inject) Controller 并调用该服务来为您进行解析。

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

关于javascript - 在 Angular JS 中使用 $parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20896870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com