简体   繁体   English

angularjs ng-show / ng-hide在用户登录后显示内容

[英]angularjs ng-show/ng-hide to display content once user is logged in

I am using Auth0 which allows me to retrieve user profile information once the login completes as follows: 我正在使用Auth0,它允许我在登录完成后检索用户配置文件信息,如下所示:

<span>Name is {{auth.profile.nickname}}</span>

// UserInfoCtrl.js
function UserInfoCtrl($scope, auth) {
  $scope.auth = auth;
}

What I want to do is use ng-show and ng-hide to only show content once a user is logged in. How have others achieved this? 我想要做的是使用ng-show和ng-hide仅在用户登录后显示内容。其他人如何实现这一目标? I was envisioning something like a conditional statement within my UserInfoCtrl.js such as: 我在UserInfoCtrl.js中设想类似于条件语句的东西,例如:

if($scope.auth !== null) {
  $scope.loggedin
}

UPDATE: Per the comment about falselys and that $scope.auth provides some value, perhaps it is better to tie something to the Login controller. 更新:根据关于falselys的评论和$ scope.auth提供一些值,也许最好将一些东西绑定到Login控制器。 Any ideas? 有任何想法吗?

// Login and logout functionality
pfcControllers.controller('loginCtrl', ['$scope', 'auth', '$location', 'store', function ($scope, auth, $location, store) {
$scope.login = function () {
    auth.signin({}, function (profile, token) {
        store.set('profile', profile);
        store.set('token', token);
        $location.path("/");
    }, function (error) {
        console.log("There was an error logging in", error);
    });
}

$scope.logout = function () {
    auth.signout();
    store.remove('profile');
    store.remove('token');
    $location.path('/login');
}
}]);

If $scope.auth has a value only when the user is logged in you could do: 如果$scope.auth仅在用户登录时$scope.auth值,则可以执行以下操作:

<span ng-show="auth">Name is {{auth.profile.nickname}}</span>

That way, you'll only see the span if auth is defined. 这样,如果定义了auth ,您将只看到span

For security reasons, I think your server should send the content of the logged area only when the user is logged. 出于安全原因,我认为您的服务器应该仅在用户登录时发送记录区域的内容。

However if, for some reason, you want do this control on the logged area you have some alternatives: 但是,如果出于某种原因,您希望对记录区域执行此控制,则可以使用以下选项:

  1. Use ng-show="auth" on your element . 在元素上使用ng-show =“auth” It will display or hide the element, according to the 'auth' value. 它将根据'auth'值显示或隐藏元素。 The element is never removed from the DOM, the frameworky simply add or remove a css class; 该元素永远不会从DOM中删除,框架只需添加或删除一个css类;
  2. Use ng-if="auth" on your element . 在元素上使用ng-if =“auth” It will add or remove the element according to the auth value. 它将根据auth值添加或删除元素。 The element is entirely removed if auth evaluates to false; 如果auth的计算结果为false,则完全删除该元素;

Both ng-show and ng-if receive an expression, the reference is here . ng-show和ng-if都接收表达式,引用在这里

Try do: 尝试做:

  1. on your controller method create: $scope.loggedin = false; 在你的控制器方法上创建:$ scope.loggedin = false;
  2. update $scope.loggedin value where it can be changed; 更新$ scope.loggedin值可以更改的位置;
  3. on the html use ng-show="loggedin" or ng-if="loggedin"; 在html上使用ng-show =“loggedin”或ng-if =“loggedin”;
  4. if the functions that changes the loggedin value was not called from angular (events...), try do $scope.$apply(function(){ .... }). 如果没有从angular(events ...)调用更改登录值的函数,请尝试执行$ scope。$ apply(function(){....})。 See here 看到这里

The simplest way to do that is putting this in your view code: 最简单的方法是将它放在您的视图代码中:

<div ng-show='auth'></div>

Now, this is not the best practice, I would recommend you to use tokens, here is post about tokens in angular 现在,这不是最好的做法,我建议你使用令牌, 这里是关于角的令牌

note: this is not a valid JS syntax: 注意:这不是一个有效的JS语法:

  if($scope.auth is not null {
     $scope.loggedin
  }

use this instead: 改用它:

  if($scope.auth != null) {
    $scope.loggedin
 }

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

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