ChatGPT解决这个技术问题 Extra ChatGPT

AngularJS:ng-show / ng-hide 不适用于`{{ }}`插值

我正在尝试使用 AngularJS 提供的 ng-showng-hide 函数显示/隐藏一些 HTML。

根据文档,这些功能的各自用法如下:

ngHide - {表达式} - 如果表达式为真,则分别显示或隐藏元素。 ngShow - {表达式} - 如果表达式为真,则元素分别显示或隐藏。

这适用于以下用例:

<p ng-hide="true">I'm hidden</p>
<p ng-show="true">I'm shown</p>

但是,如果我们使用来自对象的参数作为表达式,那么 ng-hideng-show 将被赋予正确的 true/false 值,但这些值不会被视为布尔值,因此始终返回 false

资源

<p ng-hide="{{foo.bar}}">I could be shown, or I could be hidden</p>
<p ng-show="{{foo.bar}}">I could be shown, or I could be hidden</p>

结果

<p ng-hide="true">I should be hidden but I'm actually shown</p>
<p ng-show="true">I should be shown but I'm actually hidden</p>

这要么是一个错误,要么是我没有正确执行此操作。

我找不到任何关于将对象参数引用为表达式的相关信息,所以我希望任何对 AngularJS 有更好理解的人都可以帮助我?


S
Scott

foo.bar 引用不应包含大括号:

<p ng-hide="foo.bar">I could be shown, or I could be hidden</p>
<p ng-show="foo.bar">I could be shown, or I could be hidden</p>

Angular expressions 需要在花括号绑定中,而 Angular directives 不需要。

另见Understanding Angular Templates


“Angular 表达式需要在花括号绑定中,而 Angular 指令不需要。”那条线就在那里。我希望我可以两次投票。
如果您想检查该字段是否具有值,请使用:<p ng-show="foo.bar === 'test'">I could be shown, or I could be hidden</p>
谢谢,这不是很直观(你可以从所有的赞成票中看出)
ng-hide (docs.angularjs.org/api/ng/directive/ngHide) 的文档专门将参数称为表达式,这意味着它需要大括号。我在这里想念什么?
这个答案实际上是不正确的。花括号表示应该执行表达式并将其结果插入到 DOM 中,而指令可能会也可能不会将属性值视为表达式,具体取决于其逻辑。一些指令 (ngHref) 甚至支持花括号绑定。
i
iConnor

使用角度指令与 ng-model 绑定时不能使用 {{}},但要绑定非角度属性,则必须使用 {{}}..

例如:

ng-show="my-model"
title = "{{my-model}}"

p
phellipeperin

尝试用以下方式包装表达式:

$scope.$apply(function() {
   $scope.foo.bar=true;
})

foo.bar = true 应为 scope.foo.bar = true,以更改 foo.bar 的值
我有一个奇怪的问题,有时会显示有时不会显示,将我的范围更新包装在 $scope.$apply(function () { });为我工作:)
我是 Angular 的新手,我真的不想每次需要设置变量时都这样做。有人可以解释为什么有时需要这样做吗?
helpful blog post 帮助我回答了这个问题。事实证明,任何 Ajax 或自定义侦听器都会有更新问题并需要 $scope.$apply
R
Rajkamal Subramanian

由于我认为 ng-show 是一个角度属性,我们不需要将评估花括号 ({{}})..

对于像 class 这样的属性,我们需要用评估花括号 ({{}}) 封装变量。


关闭 - 我调查了一下,似乎角 expressions 需要在大括号内,而角 directives 不需要
A
Anil Singh
<script src="http://code.angularjs.org/1.2.0-rc.2/angular.js"></script>
<script type="text/javascript">
    function controller($scope) {
        $scope.data = {
            show: true,
            hide: false
        };
    }
</script>

<div ng-controller="controller">
    <div ng-show="data.show"> If true the show otherwise hide. </div>
    <div ng-hide="!(data.hide)"> If true the show otherwise hide.</div>
</div>

V
Vijay Kumar Reddy

删除 foo.bar 周围的 {{}} 大括号,因为角度表达式不能在角度指令中使用。

更多:https://docs.angularjs.org/api/ng/directive/ngShow

例子

  <body ng-app="changeExample">
    <div ng-controller="ExampleController">
    <p ng-show="foo.bar">I could be shown, or I could be hidden</p>
    <p ng-hide="foo.bar">I could be shown, or I could be hidden</p>
    </div>
    </body>

<script>
     angular.module('changeExample', [])
        .controller('ExampleController', ['$scope', function($scope) {
          $scope.foo ={};
          $scope.foo.bar = true;
        }]);
</script>

R
Roberto

如果要根据一个 {{expression}} 的状态显示/隐藏元素,可以使用 ng-switch

<p ng-switch="foo.bar">I could be shown, or I could be hidden</p>

当 foo.bar 为 true 时,该段落将显示,为 false 时隐藏。