I am working on a application which uses angularjs. In this i have a login page and home page.
Whole page is divided into two parts header and container each with headerCtrl and loginCtrl respectively.
header is same for both parts and only rest part changes after login.
This is 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);
};
}
]);
This is 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('/');
}
});
};
});
This is 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>
How can we write headerCtrl and what changes are required in login controller and service so that i can access the user details(username) in header.html file.