简体   繁体   English

当用户的角色是雇员时,使用Ng-show或ng-if隐藏按钮

[英]Ng-show or ng-if to hide a button when the role of the user is employee

Trying to figure out ng-show and ng-if. 试图找出ng-show和ng-if。 I want to show a button only when you are logged in as a admin. 我只想在以管理员身份登录时显示一个按钮。 If the user is employee then the button has to be hidden. 如果用户是雇员,则该按钮必须隐藏。

Part of my html code: 我的html代码的一部分:

 <md-button ng-show="user.role == 'admin'" class="md-raised md-primary pull-left customYellow" aria-label="Add a user" ng-click="showPopUpAddUser($event)">
        <md-icon md-svg-src="assets/images/person_add.svg"></md-icon>
        <md-tooltip>
         Invite a user
        </md-tooltip>
     </md-button>

Part of my app.js code: 我的app.js代码的一部分:

$scope.inviteUser = function(){
    $http.post('/api/users/invite', {
        'email': $scope.user.email,
        'role_id': $scope.user.role
    }, {
        headers: {
            "Content-Type": "text/plain"
        }
    })
    .success(function(data, status, headers, config) {
        console.log("Successfully saved a user to the DB", data);

        $scope.userInfo.push(data);

    })
    .error(function(data, status, headers, config) {
        console.log("Failed to add user to DB");
    });
}

 $scope.adminID = function(id) {
            var adminId = $scope.getCurrentUser().role_id;
            console.log("logging id admin-->", adminId);

            //$scope.user = { role : false };

            $scope.userRole = adminId;
            //console.log("fafdasfdsafadfdfsf---------->",  $scope.userRole)
        }
        $scope.adminID();

Try like this 这样尝试

ng-show="user.role == 'admin'"

You forget to add '' in your code 您忘记在代码中添加''

I'm not a huge fan of logic on the view. 我不是逻辑上的忠实粉丝。 This is just a personal preference but i would abstract the === into a filter: 这只是个人喜好,但我会将===抽象化为过滤器:

app.filter('equals', function () {
   return function (actual, expected) {
       return actual === expected;
   };
});

then on the view: 然后在视图上:

ng-show="admin.role | equals:'admin'"

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

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