0

I have this code in controller `

(function () {
'use strict';
var LoginController = function($scope, $http, $timeout,$location,User) {
    $scope.user = {};
    $scope.flags = {
        error: false,
        rememberMe: true
    };
    $scope.signin = function() {
        $scope.errorMsg=false;
        $scope.successMsg=false;
        $scope.flags.loginInProgress=true;

        User.login($scope.user.password,$scope.user.email)
            .then(function(response) {

                if (response.data.success) {
                    $scope.successMsg=response.data.message;

                    $scope.flags.loginInProgress=true;
                    $timeout(function () {
                       $location.path('shop');
                    },2000);
                }else{
                    $scope.flags.loginInProgress=false;

                    $scope.errorMsg=response.data.message;

                    $scope.flags.error = true;
                };
            });
    };
    if (User.isLoggedIn()) {
        User.getUser().then(function (response) {
                $scope.user=response.data.user;
        })

    } else {
        $location.path('login');
    }
    $scope.logout=function () {
        User.logout();
        $timeout(function () {
            $location.path('home');
        },2000);
    };
};

LoginController.$inject = [
    '$scope',
    '$state',
    '$timeout',
    '$location',
    'User'
];

angular
    .module('pharmacy')
    .controller('LoginController', LoginController)
}());`

I use ui-router and i use {{user.name}} in page where LoginController is declared it is working but i use {{LoginController.user.name}} where controller is not declared it is doesn't working.

What could be the problem?

2
  • it sounds like you are misunderstanding how $scope works. you don't have a property called $scope.LoginController. so {{LoginController}} wouldn't be valid. aside from that, you say "where controller is not declared"; I would assume this means in HTML that doesn't have ng-controller, which wouldn't work with angular at all. Commented Feb 11, 2017 at 19:46
  • Have you solved your issue? If yes, can you post the answer here? Commented Feb 19, 2017 at 22:35

2 Answers 2

0

Just Try controllerAs syntax as provided by angular 1.5

<div ng-controller="loginController as login">
{{login.user.name}}
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can only access to a controller's scope in the view where that controller is. If you want to access to user's information in other places of the application you should store it elsewhere, a provider (factory for example).

Check the following jsbin: http://jsbin.com/vomifoyuqo/1/edit?html,js,console

var app = angular.module("AngularApp", ['test']);

angular.module('test', [])
    .controller('LoginController', ['$scope', 'AuthFactory', function($scope, AuthFactory) {
        $scope.user = AuthFactory.user;

        AuthFactory.signin();
    }])
    .controller('OtherController', ['$scope', 'AuthFactory', function($scope, AuthFactory) {
        $scope.user = AuthFactory.user;
    }])
    .factory('AuthFactory', ['$timeout', function($timeout) {
        var user = {};

        var signin = function() {
            console.log('signin');
            $timeout(function() {
                user.name = 'TEST';
                console.log('user name set');
            }, 500);
        };

        return {
            signin: signin,
            user: user
        };
    }]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.