javascript - 从 Controller 设置 ng-show 值但我的代码不工作

标签 javascript jquery html angularjs node.js

Angular js 的新手。我阅读了很多关于我的代码的解决方案。我面临的问题是,我有两张 table 。默认情况下,一张表是隐藏的,一张是可见的。当我点击按钮并想隐藏第二个表时,我想显示隐藏的表。当我单击按钮时,ng-show 值已更新,但表格未隐藏且所需表格未显示。在此先感谢您的帮助。

页面加载代码:controller.js

var refresh = function(){
            /** Driver Profile Data */
            $scope.userProfile_show  = true;
            $scope.editUserProfile_show  = false;
            $http.get('/user/profile/?user_id='+$location.search()['driverID'], {headers:{'Authorization': 'Bearer ANmI3xaBz3MQMbrOs3XAtuAwEgZt1pbOhSMGfHHTxwhgyi4SJfmmOGcxN5bEwhFh26TyfTgKxhtHpX9rGPWHgtB7D3LBex5mDkxBL7LyhvQ2MjPNBkeNZyUBgBuhO1IK9n13YHdpfbnMdnep4PRnDHScMWmJi1kV0NxkgiIsEC3x0pHZxhhLvgEIEqF6qGVLPSXN010Em8rzXraPGV9NyG6t6a0zYYlDKONYvQ7FnwP1p67ViTu2wUgbilnwoymQ'}
            }).success(function (response) {
                $scope.userProfile = response.data;
            });

            });
        }

        refresh();

HTML 代码:

<table class="table" id = "editUserProfile" ng-show = "{{editUserProfile_show}}">
                <thead>
                    <tr>
                        <td>Property</td>
                        <td>Value</td>
                    </tr>
                </thead>
<table>

<table class="table" id = "userProfile" ng-show= "{{userProfile_show}}">
                <colgroup>
                    <col class="col-md-5">
                    <col class="col-md-7">
                </colgroup>
                <thead>
                    <tr>
                        <th>User Profile</th>
                        <th style="text-align: -webkit-right"><button class="btn-primary btn" id = "editUserProfile_button" ng-click = "editUserProfile()">Edit User Profile</button></th>
                    </tr>
                </thead>
<table>

在页面加载时,两个表都正常工作,但是当单击“编辑用户配置文件”按钮时,ng-show 的值会更新,但表不会切换其可见性。

editUserProfile() 的 Controller 代码

$scope.editUserProfile = function(){
            $scope.editUserProfile_show  = true;
            $scope.userProfile_show  = false;
            $http.get('/user/profile/?user_id=559f6d43cd9e9cfd07422baa', {headers:{'Authorization': 'Bearer ANmI3xaBz3MQMbrOs3XAtuAwEgZt1pbOhSMGfHHTxwhgyi4SJfmmOGcxN5bEwhFh26TyfTgKxhtHpX9rGPWHgtB7D3LBex5mDkxBL7LyhvQ2MjPNBkeNZyUBgBuhO1IK9n13YHdpfbnMdnep4PRnDHScMWmJi1kV0NxkgiIsEC3x0pHZxhhLvgEIEqF6qGVLPSXN010Em8rzXraPGV9NyG6t6a0zYYlDKONYvQ7FnwP1p67ViTu2wUgbilnwoymQ'}
            }).success(function (response) {
                console.log(response.data);
                $scope.userProfile = response.data;
            });
        };

HTML 点击后查看功能:

<table class="table ng-hide" id="editUserProfile" ng-show="true">
                <thead>
                    <tr>
                        <td>Property</td>
                        <td>Value</td>
                    </tr>
                </thead>
</table>

<table class="table" id="userProfile" ng-show="false">
                <colgroup>
                    <col class="col-md-5">
                    <col class="col-md-7">
                </colgroup>
                <thead>
                    <tr>
                        <th>User Profile</th>
                        <th style="text-align: -webkit-right"><button class="btn-primary btn" id="editUserProfile_button" ng-click="editUserProfile()">Edit User Profile</button></th>
                    </tr>
</table>

当我点击按钮时,ng-hide 类被添加到第一个表 editUserprofile 中。当我删除 ng-hide 类时,该表是可见的,但第二个表仍然可见,尽管它的 ng-show 是假的。预计,问题与 $scope 有关。

最佳答案

您不需要字符串插值,即 {{ }}ngShow指令,它需要一个 expression .如果expressiontruthy 则元素分别显示或隐藏。

如果您传递任何字符串值 "true""false",它将评估为 truthy 因此元素将始终显示。

使用

ng-show = "editUserProfile_show"

代替

ng-show = "{{editUserProfile_show}}"

关于javascript - 从 Controller 设置 ng-show 值但我的代码不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31716537/

相关文章:

javascript - JavaScript fetch()无法捕获404错误

javascript - 有没有办法将 .zip 中的图像放入 html 中?

javascript - 如何从jquery对象中删除元素

javascript - 如何在javascript中的两个分隔符之间拆分字符串?

javascript - 在 html 输入日期时间本地选择器中禁用前一天的前一个时间

javascript - Angular - 我是否需要为每个 $http 调用使用 promise ?

javascript - 如何从 jquery 中同一类的成员元素中获取所有不同的值?

jquery - Cakephp 中的类似对话框的工具提示 - 类似于 stackoverflow 框

jquery - 我的横幅图像不会改变我在 jQuery 脚本中设置的速度和数据的横幅

css - 在 IE 中背景渐变没有延伸到底部