-1

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.

ashishkumar148
  • 975
  • 1
  • 10
  • 26

2 Answers2

0

It might help you!!

Your HTML code-sample

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

Your AngularJs code-sample

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('/');
            }
        });
    };
});
Anil Singh
  • 4,173
  • 4
  • 41
  • 47
0

U can use a broadcast on the $rootscope from login controller when ur lgon service completes and listen to the use event in headercontroller.the event argument you can pass as the user details.

Rahul Ravindran
  • 318
  • 3
  • 11