javascript - 在将范围变量传递给 ng-show 之前如何评估范围变量

标签 javascript angularjs

Controller

scope.x = 'y';
scope.y = true;

模板

<div ng-show="{{x}}"></div>

结果应该是一个可见的 div 但它是隐藏的

但是 html 显示了我所期望的

<div ng-show="y"></div>

在我实际的更复杂的例子中我得到了这个错误

TypeError: Cannot read property 'exp' of undefined
    at watchFnToHumanReadableString (<anonymous>:703:19)
    ...

this plunkr show the simple example

最佳答案

要评估作用域变量,您需要从 $scope 调用 $eval 函数。

您的代码应如下所示:

<body ng-controller="MainCtrl">
<p ng-show="$eval(x)">Hello {{name}}!</p>
</body>

关于javascript - 在将范围变量传递给 ng-show 之前如何评估范围变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22336524/

相关文章:

javascript - 将圆形边框转换为可点击功能

android - 使用 Ionic 在 Android/iOS 上执行外部应用程序

javascript - 停止在 Angular.js 中缓存 View

javascript - 如何将这个 moment.js 代码从 node.js 运行到 angular.js Controller ?

angularjs - 如何使用angularjs设置下拉默认值

javascript - Vis.js 网络节点定制 : cards as nodes

javascript - 防止 IE9 中的默认键处理

javascript - 使用jquery在html中搜索文件名

javascript - 用于设计查询语言的正则表达式

javascript - Bootstrap 模式无法在 ng-include 内工作