2016-03-08 29 views
0

如何仅在用户登录到站点时显示菜单列表。但截至目前,用户能够通过直接提供URL来查看菜单。如果用户登录那么只有他能够看到菜单列表和所有。可以任何人检查这一点。 HTML代码:只有在登录后才能在angularjs中显示特定页面

<html lang="en" ng-app="accountantApp"> 
<div class="container jumbotron" ng-init= "getAddressInfo()" ng-controller="AddressInfoController" id="homejumbotron"> 
    <div class="col-sm-12 primary"> 
     <div class="col-sm-2"><a href="#" class="ui-btn ui-shadow ui-corner-all personal">PERSONALINFO</a></div> 
     <div class="col-sm-2"><a href="login.php" class="ui-btn ui-shadow ui-corner-all">LOGOUT</a></div> 
    </div> 

JS:

var app = angular.module('accountantApp', ['ui.bootstrap']); 
app.controller('AddressInfoController', function($scope,$http,$timeout) { 
$scope.submitAddressInfo = function(isValid, user) { 
    if (isValid) { 
     console.log("address info::"+$scope.user); 
     $http({ 
       method : 'POST', 
       url  : '../model/addaddressinfo.php',    
       headers : { 
          'Content-Type': 'application/json' 
          }, 
       data  : {'addressinfo': $scope.user, 'action': 'Save'} 

      }).success(function(data, status, headers) { 

       console.log("Response data:"+ data.error); 
       if (data.success != undefined && data.success != '') 
       { 
        console.log("inside success"); 
        window.location.href = "income_source.php"; 
        $scope.getTemplate("ADDRESSINFO"); 
        $scope.user = ''; 

       } 
       else 
       { 
        $scope.error = data.error; 
       }   

      }).error(function(data, status, headers) { 
       window.location.href = "error.html"; 
       console.log("Error data::::"+ data); 

       alert("Error occured while creating address info:"+status); 
         });   
     } 
     else{ 
     $scope.submitted = true; 
     alert("Fill the form."); 
     return; 
    } 

}; 
+1

假设你有一个的LoginController在你的范围(例如'isAuthenticated')添加认证标志,一旦用户登录这个标志设置为true 。在你的意见中添加'ng-hide'指令到你的链接并使用isAuthenticated标志的值 – Sherlock

+0

@EuphoriaGrogi我不明白你在说什么 – user5751258

+0

@EuphoriaGrogi你能不能更新我一个例子 – user5751258

回答

0

问题有点不清楚,您发布的控制器代码似乎是不相关的。

但假设你想要做的就是根据用户是否登录显示/隐藏某些内容,那么你可以像EuphoriaGrogi所说的那样做,创建一个变量,如果用户登录如果不是,则为false。类似这样的:

$http.post('login_url').then(function(response){ 
     if(login_successful){ 
      $rootScope.isAuthenticated = true; 
     } 
     else{ 
      //handle error 
     } 
    }); 

当您注销时将其设置为false。在HTML中使用的NG-如果在任何你想隐藏:

<div class="col-sm-12 primary" ng-if="isAuthenticated"> 
     <div class="col-sm-2"><a href="#" class="ui-btn ui-shadow ui-corner-all personal">PERSONALINFO</a></div> 
     <div class="col-sm-2"><a href="login.php" class="ui-btn ui-shadow ui-corner-all">LOGOUT</a></div> 
    </div> 
+0

$ http.post('login_url')。然后(功能(响应){这里你给的login_url,但从我们必须给login_url函数 – user5751258

+0

所以你没有' t在你的应用程序中实现了登录/注销功能吗? –

+0

Ya我在php中实现了登录,注册功能,它在php中工作,现在我正在使用angularjs – user5751258

相关问题