2015-01-14 4 views
-1

나는 angularjs를 사용하는 응용 프로그램에서 작업하고 있습니다. 이 로그인 페이지 및 홈 페이지 있습니다.
전체 페이지의 두 부분 헤더 각각 headerCtrlloginCtrl와 용기 각각으로 분할된다. 헤더는 두 부품 모두 동일하며 로그인 후 나머지 부품 만 변경됩니다.다른 컨트롤러에서 컨트롤러의 데이터에 액세스

이것은 loginCtrl입니다.

angular.module('app').controller('LoginCtrl', ['$scope', 'LoginService', 
    function($scope, LoginService) { 
     $scope.title = "Login"; 

     $scope.login = function() { 
      var user = { 
       username: $scope.username, 
       password: $scope.password 
      }; 
      LoginService(user); 
     }; 

    } 
]); 

이것은 loginService입니다.

angular.module('app') 
.factory('LoginService', function($http, $location, $rootScope) { 
    return function(user) { 
     $http.post('/login',{ 
       username: user.username, 
       password: user.password 
      }).then(function(response) { 
      if (response.data.success) { 
       console.log(response.data); 
       $rootScope.user = response.data.user; 
       $location.url('/'); 
      } else { 
       console.log(response.data.errorMessage); 
       $location.url('/'); 
      } 
     }); 
    }; 
}); 

이 HEADER.html 현재입니다

<div ng-controller="headerCtrl" class="container-fulid"> 
    <div class="row custom-row1"> 
     <div><span class="title_top pull-right">{{ user }}</span></div> 
    </div> 
</div> 

우리는 headerCtrl을 쓸 수와 내가 HEADER.html 현재의 사용자 정보 (사용자 이름)에 액세스 할 수 있도록 어떤 변화가 로그인 컨트롤러 및 서비스에서 요구되는 방법 파일.

+0

헤더 컨트롤러가 로그인 컨트롤러에 있으면 – Antguider

+0

에 직접 액세스하여 더 자세한 정보를 제공 할 수 있습니다. 다른 컨트롤러 내부에 컨트롤러를 정의 할 수 있습니까? – ashishkumar148

+0

예! 하나의 컨트롤러를 다른 컨트롤러에 사용할 수 있습니다. –

답변

0

도움이 될 수 있습니다! U가 사용 UR lgon 서비스가 완료되면 로그인 컨트롤러에서 $의 rootscope에 브로드 캐스트를 사용하여들을 수 있습니다

var baseURL = "http://localhost:56110/"; 
var app = angular.module('loginApp', []); 

app.service('loginService', function ($http) { 
    this.login = function (user) { 
     return $http.post('/login', user) 
      .success(function (data, status, headers, config) {    
      }) 
      .error(function (data, status, headers, config) {    
      }); 
      } 
     }); 
    }; 
}); 

app.controller('loginCtrl', function ($scope, loginService) { 

    $scope.title = "Login"; 
    $scope.uid = 123; 
    $scope.pwd = 123; 

    $scope.login = function() { 
     var pwd = { 
      username: $scope.uid, 
      password: $scope.pwd 
     }; 

     loginService.login(pwd).then(function (response) {    
      if (response.data.success) { 
       console.log(response.data); 
       $rootScope.user = response.data.user; 
       $location.url('/'); 
      } 
      else { 
       console.log(response.data.errorMessage); 
       $location.url('/'); 
      } 
     }); 
    }; 
}); 
+0

로그인이 html의 제목으로 표시됩니다. 하지만 나는 그 장소에 uid가 오길 원합니다. – ashishkumar148

+0

uid를 제목 자리에 넣을 수 있습니다. –

0

귀하의 HTML 코드 샘플

<div ng-controller="loginCtrl" class="container-fulid"> 
    <div class="row custom-row1"> 
     <div><span class="title_top pull-right">{{ title }}</span></div> 
    </div> 
</div> 

귀하의 AngularJS와 코드 샘플 이벤트를 사용자 세부 정보로 전달할 수있는 headercontroller.the 이벤트 인수

관련 문제