gpt4 book ai didi

javascript - Angular如何让html将作用域变量渲染为字符串

转载 作者:太空宇宙 更新时间:2023-11-04 16:31:07 25 4
gpt4 key购买 nike

假设我在 Controller hmmCtrl 中有以下行:

$rootScope.value = 1;
$scope.hmm = "{{$root.value}}"

在 html 中,如果我有:

<section ng-controller="hmmCtrl">
{{hmm}}
</section>

当前显示:

{{$root.value}}

但我实际上想查看 $root.value 的值:

1

从长远来看,我计划将 $root.value 放入将由 hmmCtrl 解析的 json 文件中。

我怎样才能做到这一点?

最佳答案

所以你可以做的是,你可以在 Controller 内部编写一个可以返回插值表达式评估值的 Controller

代码

//inject `$interpolate` inside controller function before using it.
$scope.evaluateValue = function(expr){
return $interpolate(expr)($scope);
}

标记

<section ng-controller="hmmCtrl">
{{evaluateValue(hmm)}}
</section>

其他方式

<section ng-controller="hmmCtrl" ng-bind="evaluateValue(hmm)">
</section>

Demo Here

关于javascript - Angular如何让html将作用域变量渲染为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39800594/

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