【问题标题】:ng-hide & ng-show not working only in a particular AngularJS pageng-hide & ng-show 仅在特定的 AngularJS 页面中不起作用
【发布时间】:2016-01-05 18:16:59
【问题描述】:

对于我正在构建的设备的详细信息视图页面,我使用了 Angular 的 ng-showng-hide 指令来根据设备状态颜色更改按钮显示。我使用 {{item.transaction_Mode}} 从数据库中获取颜色。 Angular 部分似乎可以正常工作,因为它可以正确输出“true”和“false”,但是当它与 ng-show 结合使用时,例如 ng-show='true',它就不起作用了。

HTML 代码:-

颜色生成部分-

<div  style="width: 30%; float: left">
                    <div ng-show="{{item.transaction_Mode == 'red'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: red;float: left; " > </div>     
                    <div ng-show="{{item.transaction_Mode == 'orange'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: orange;float: left; " > </div>     
                    <div ng-hide="{{item.transaction_Mode == 'red'}} || {{item.transaction_Mode == 'orange'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: green;float: left; " > </div>     
                    <h1  style="text-align: left">{{devices[whichItem].name}} - [id  {{devices[whichItem].device_ID}}] </h1>
</div>

按颜色部分显示按钮-

<div  style="width: 70%; float: left; "> 


                   <button ng-hide="{{item.transaction_Mode == 'red'}} || {{item.transaction_Mode == 'orange'}}" style="margin-top:  20px; width: 17%; float: left; height: 46px;" type="button" data-target="modal1" class="Now waves-effect waves-light btn modal-trigger" >Now</button>
                    <p style="width: 3%; float: left"> </p>
                    <button  style="margin-top:  20px; width: 17%; float: left; height: 46px;" type="button" data-target="modal2" class="Later waves-effect waves-light btn modal-trigger ">Later</button>
                    <p style="width: 3%; float: left"> </p>
                    <button ng-show="{{item.transaction_Mode == 'orange'}}" style="margin-top:  20px; width: 17%; float: left; height: 46px;" type="button" class="waves-effect waves-light btn " >Get</button>
                    <p style="width: 3%; float: left"> </p>
                    <button ng-show="{{item.transaction_Mode == 'orange'}}" style="margin-top:  20px; width: 17%; float: left; height: 46px; " type="button" data-target="modal4" class="Cancel waves-effect waves-light btn modal-trigger" >Cancel</button>
                    <p style="width: 3%; float: left"> </p>
                    <button ng-show="{{item.transaction_Mode == 'red'}}" style="margin-top:  20px; width: 17%; float: left; height: 46px; " type="button" data-target="modal3" class="Return waves-effect waves-light btn modal-trigger" >Return</button>

</div>

但是同样的代码适用于另外两页,我不明白为什么它不适用于这个。 ng-if 或 ng-switch-when 也不起作用。 任何帮助将不胜感激,在此先感谢!

【问题讨论】:

  • 您是否也在此页面中包含了控制器?
  • 尝试使用ng-if来克服它!
  • 你有让这些元素可见的 CSS 样式吗? ngShowngHide 使用 display: none
  • 去掉花括号。例如:将 ng-show="{{item.transaction_Mode == 'orange'}}" 更改为 ng-show="item.transaction_Mode == 'orange'"
  • 我的控制器如下 - homeCtrls.controller('detailsCtrl', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) { $http.get('webapi/devices').success(function (data) { $scope.devices = data; $scope.whichItem = $routeParams.itemId; });

标签: javascript jquery angularjs ng-hide


【解决方案1】:

错字。 ng-if、ng-show 不需要 {{}} 绑定到 $scope 数据。

所以,

<div  style="width: 30%; float: left">
                    <div ng-show="{{item.transaction_Mode == 'red'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: red;float: left; " > </div>     
                    <div ng-show="{{item.transaction_Mode == 'orange'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: orange;float: left; " > </div>     
                    <div ng-hide="{{item.transaction_Mode == 'red'}} || {{item.transaction_Mode == 'orange'}}" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: green;float: left; " > </div>     
                    <h1  style="text-align: left">{{devices[whichItem].name}} - [id  {{devices[whichItem].device_ID}}] </h1>
</div>


应该是:

<div  style="width: 30%; float: left">
                    <div ng-show="item.transaction_Mode == 'red'" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: red;float: left; " > </div>     
                    <div ng-show="item.transaction_Mode == 'orange'" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: orange;float: left; " > </div>     
                    <div ng-hide="item.transaction_Mode == 'red' || item.transaction_Mode == 'orange'" class="col s1 m1 l1 vertical-line" style="height: 100%;width: 10px; background-color: green;float: left; " > </div>     
                    <h1  style="text-align: left">{{devices[whichItem].name}} - [id  {{devices[whichItem].device_ID}}] </h1>
</div>

【讨论】:

  • 嘿,谢谢,但这并不能解决我的问题。之所以使用{{}},是因为我已经定义了变量item,并且从前端获取了每个item相关的transaction_Mode(颜色),所以可以直接使用。 Angular 表达式确实会输出正确的答案,如“真”或“假”,但是当它带有 ng-show / ng-hide 时,它​​就不起作用了。
【解决方案2】:

代替:

ng-show="{{item.transaction_Mode == 'orange'}}"

用途:

ng-show="item.transaction_Mode == 'orange'"

【讨论】:

  • 嘿,谢谢,但这并不能解决我的问题。之所以使用{{}},是因为我已经定义了变量item,并从前端获取了与每个item相关的transaction_Mode(颜色),所以可以直接使用。 Angular 表达式确实会输出正确的答案,如“真”或“假”,但是当它带有 ng-show / ng-hide 时,它​​就不起作用了。
【解决方案3】:

AngularJs 指令,例如 ng-show 或 ng-hide 不需要使用 {{}}。

【讨论】:

  • 嘿,谢谢,但这并不能解决我的问题。之所以使用{{}},是因为我已经定义了变量item,并从前端获取了与每个item相关的transaction_Mode(颜色),所以可以直接使用。 Angular 表达式确实会输出正确的答案,如“真”或“假”,但是当它带有 ng-show / ng-hide 时,它​​就不起作用了。
猜你喜欢
  • 2015-06-29
  • 2014-12-08
  • 2014-03-27
  • 2015-01-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 2015-07-22
相关资源
最近更新 更多