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

标签 javascript angularjs

我正在使用 Auth0,它允许我在登录完成后检索用户个人资料信息,如下所示:

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

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

我想做的是使用 ng-show 和 ng-hide 仅在用户登录后显示内容。其他人是如何做到这一点的?我在我的 UserInfoCtrl.js 中设想了类似条件语句的东西,例如:

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

更新:根据关于 falselys 的评论和 $scope.auth 提供了一些值(value),也许最好将某些东西绑定(bind)到登录 Controller 。有什么想法吗?

// 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');
}
}]);

最佳答案

如果 $scope.auth 只有在用户登录时才有值,你可以这样做:

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

这样,如果定义了 auth,您将只会看到 span

关于javascript - angularjs ng-show/ng-hide 在用户登录后显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27904596/

相关文章:

javascript - 在 AngularJS 中将对象推送到 $scope.array 与 var x = [ ] 时是否存在性能问题?

javascript - javascript函数执行后jquery不工作

javascript - AWS Cognito 身份验证返回错误 - Javascript SDK

javascript - 等待 Promise 在 emitter.on 监听器函数中解析

angularjs - 递归 ui 路由器嵌套 View

javascript - 使用 Asp.net 和 angularjs 的 Odata

javascript - 如何使用angularfire通过id检索数据

javascript - 将 JS 对象转换为具有父名称的平面数组

Javascript 整数到时间字符串

javascript - 使用循环计算数组中新推送的数据