[英]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: 但是,如果出于某种原因,您希望对记录区域执行此控制,则可以使用以下选项:
Both ng-show and ng-if receive an expression, the reference is here . ng-show和ng-if都接收表达式,引用在这里 。
Try do: 尝试做:
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.