简体   繁体   English

单击按钮后,在Angular中隐藏按钮并显示数据

[英]Hide button and show data, upon button click, in Angular

It probably goes without saying that I'm quite new to angular as I'm trying to accomplish a relatively simple task, and I've come here for some help 毋庸置疑,我在尝试完成一个相对简单的任务时对角刚还很陌生,我来这里寻求帮助

I'm recreating our company's password vault using angular. 我正在使用angular重新创建我们公司的密码库。

Here is what I am trying to accomplish. 这是我要完成的工作。

The page loads with a list of accounts. 该页面加载有帐户列表。 Most the information is visible except for the password. 除密码外,大多数信息都是可见的。 I have a button that when clicked, hides the button, queries the database, logs who queried password, and displays the password to the user. 我有一个按钮,单击该按钮时会隐藏该按钮,查询数据库,记录谁查询密码并向用户显示密码。 The passwords are clear text because they aren't passwords for client accounts or anything sensitive, it exists for our employees to reference how/where to login to various websites we use for day to day business. 密码是明文,因为它们不是客户帐户或任何敏感密码,我们的员工可以参考该密码来/如何登录到我们用于日常业务的各种网站。

My HTML looks as follows: 我的HTML如下所示:

    <tr ng-repeat="account in resp.PasswordVaultAccounts">
        <td><a href="{{account.URL}}" target="_blank">{{account.Name}}</a></td>
        <td>{{account.Username}}</td>
        <td><button ng-click="showPassword(account.AccountId);" class="btn btn-primary">View</button><span></span></td>
        <td>{{account.Comments}}</td>
    </tr>

My scope controller looks as follows 我的范围控制器如下所示

$scope.showPassword = function (accountId) {
        passwordVaultData.getAccountPassword(accountId)
            .$promise
            .then(function (r) {
                   //success
            }, function (r) {
                  //fail
            });
    }

My showPassword() method works and returns the correct password, but I can't figure out how to hide the button and display the password. 我的showPassword()方法可以正常工作并返回正确的密码,但是我不知道如何隐藏按钮并显示密码。

Using the ng-show and ng-hide directives against the password on the account object should suffice for modifying the UI 对帐户对象上的密码使用ng-show和ng-hide指令应足以修改UI

<tr ng-repeat="account in resp.PasswordVaultAccounts">
    <td><a href="{{account.URL}}" target="_blank">{{account.Name}}</a></td>
    <td>{{account.Username}}</td>
    <td>
      <button ng-hide="account.Password" ng-click="showPassword(account.AccountId);" class="btn btn-primary">View</button>
      <span ng-show="account.Password">{{account.Password}}</span>
    </td>
    <td>{{account.Comments}}</td>
</tr>

As for the promise resolution, you want the getAccountPassword to return a promise, I will make an assumption about it's content below 至于promise解析,您希望getAccountPassword返回一个promise,我将在下面对其进行假设

function getAccountPassword(account) {
   var deferred = $q.defer();
   $http.get('api/vault/' + account.AccountId).then(function(r) {
      deferred.resolve(r);
   }, function(r) {
      deferred.reject(r);
   });
   return deferred.promise;
}

$scope.showPassword = function (account) {
    getAccountPassword(account.AccountId).then(function(password) {
      account.Password = password;
    }, function(password) {
      account.Password = undefined; // some type of error handling here
    });
 }

Because the promise is executed in the context of an $http call, the digest cycle will run and the elements will be shown based on whether password is populated. 由于promise是在$ http调用的上下文中执行的,因此摘要循环将运行,并且将根据是否填充密码来显示元素。

You can accomplish it by either ng-if or ng-show/hide: 您可以通过ng-if或ng-show / hide来实现:

Quick sample below: 以下是快速示例:

 <tr ng-repeat="account in resp.PasswordVaultAccounts">
    <td><a href="{{account.URL}}" target="_blank">{{account.Name}}</a></td>
    <td>{{account.Username}}</td>
    <td>
       <button ng-if="!account.password" ng-click="showPassword(account);" class="btn btn-primary">View</button><span></span></td>
       <span ng-if="account.password">{{password}}</span>
    <td>{{account.Comments}}</td>
</tr>


$scope.showPassword = function (account) {
    account.password = passwordVaultData.getAccountPassword(account.AccountId)
        .$promise
        .then(function (r) {
               //success
        }, function (r) {
              //fail
        });
}

Please see demo below 请看下面的演示

 var app = angular.module('app', []); angular.module('app'). controller('firstCtrl', function($scope) { $scope.resp = { PasswordVaultAccounts: [{ AccountId: 1, URL: "bbc.co.uk", Username: "Jack", Comments: "White" }, { AccountId: 2, URL: "bbc.co.uk", Username: "Mike", Comments: "Green" }, { AccountId: 3, URL: "bbc.co.uk", Username: "Tim", Comments: "Red" } ] } $scope.showPassword = function(account) { //call you backend and on sucess add that : // passwordVaultData.getAccountPassword(account.accountId) // .$promise // .then(function (r) { account.showpass = true; account.pass = account.Username + " password is *****" } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <body ng-app="app"> <div ng-controller="firstCtrl"> <table> <tr ng-repeat="account in resp.PasswordVaultAccounts"> <td><a href="{{account.URL}}" target="_blank">{{account.Name}}</a> </td> <td>{{account.Username}}</td> <td> <button ng-click="showPassword(account);" class="btn btn-primary" ng-hide="account.showpass">View</button> <span ng-show="account.showpass">{{account.pass}}</span> </td> <td>{{account.Comments}}</td> </tr> </table> </div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM