【问题标题】:ng-show not working on angularjs the div is still hiddenng-show 不适用于 angularjs div 仍然隐藏
【发布时间】:2019-12-05 09:38:46
【问题描述】:

我将响应数据分配给 $scope 但带有 ng-show 的 div 仍然隐藏。我是 angularjs 的菜鸟,怎么了??

else {
         debugger;
         $scope.exception = data.exception; //data arrives correctly

和html

<div class="error-text"  ng-show="exception.code">

但它不起作用并且控制台上没有显示错误

【问题讨论】:

  • "ng-show" 需要布尔值,那么 exception.code 的值是什么?
  • 它是一个字符串不评估它是否为空吗?
  • 可以分享更多的js控制器代码吗?
  • @afccc ,这是正确的,null 将评估为假。问题出在其他地方。
  • @Piyush 所有值都是truthy,除非它们被定义为falsy。 JavaScript 中的真值示例:3.14、"false"、{}、"string" 等

标签: javascript html angularjs


【解决方案1】:

ng-show 指令作用于truthy 或falsy 值。

如果exception 缺少代码属性,则该元素不会显示自己。

试试:

 ̶<̶d̶i̶v̶ ̶c̶l̶a̶s̶s̶=̶"̶e̶r̶r̶o̶r̶-̶t̶e̶x̶t̶"̶ ̶ ̶n̶g̶-̶s̶h̶o̶w̶=̶"̶e̶x̶c̶e̶p̶t̶i̶o̶n̶.̶c̶o̶d̶e̶"̶>̶
 <div class="error-text"  ng-show="exception">
     exception = {{exception}}
 </div>

有关详细信息,请参阅

【讨论】:

  • 问题是异常有属性码但是没有显示html
【解决方案2】:

'ng-show' 作用于布尔值,所以你只需要设置 $scope.exception.code = true;,确保你的对象在异常中有一个名为“代码”的布尔键。

所以你的 JS 代码会是这样的:

else {
         debugger;
         $scope.exception = data.exception;//data arrives correcly
         $scope.exception.data = true;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多