user3568783
user3568783

Reputation:

How can I pass down a value in the $scope to an inner controller in AngularJS?

In my angular app.js that's the main controller in my index.html I want to have a function like this:

    $scope.login = function (user) {
        $scope.authenticating = true;
        var config = {
            method: 'POST',
            url: '/api/Account/Login',
            data: {
                'userName': user.loginUserName,
                'password': user.loginPassword,
                'rememberMe': user.loginRememberMe
            }
        };
        $http(config)
            .success(function (data) {         
                authentication.isAuthenticated = true;
                authentication.userName = user.loginUserName;
                localStorage.isAuthenticated = 'yes';
                $scope.template = $scope.templates[1];
                $state.transitionTo('home');
            })
    };

In my controllers that are for templates inside index.html I want to be able to get access to the userName. Can someone tell me how I can do this? Initially I tried setting $scope.user = {} in the app.js and then setting $scope.user.userName but when I open another template and its controller that's inside app.js the $scope.user does not seem to be visible.

Upvotes: 0

Views: 311

Answers (2)

Didar_Uranov
Didar_Uranov

Reputation: 1240

All child controllers inherit $scope variables from parents

<div ng-controller="c1">
    <div ng-controller="c2"></div>
</div>

youApp.controller('c1', ['$scope', function($scope){
    $scope.blabla = 1;
}]);

youApp.controller('c2', ['$scope', function($scope){
    // $scope.blabla is also 1 here because c2 is a child of c1
}]);

You can store anydata in built-in $rootScope service. Services are singleton in Angular

yourApp.run(['$rootScope', functin($rootScope){
    $rootScope.foo = 'bar';
    // you can inject $rootScope in any controller you want and use $rootScope.foo
}]);

As @Robin suggest, I also recommend to write your own service for authorization

Upvotes: 1

Robin Drexler
Robin Drexler

Reputation: 4457

You could use a service to share data between controllers

app.service('userService', function() {
  var user = {};

  setUserName = function(name) {
    user.name = name;
  };

  getUser = function(){
      return user;
  };
});

Controller that sets data

app.controller('FooController', ['$scope', 'userService',
    function ($scope, userService) {
        $scope.login = function (user) {
            $scope.authenticating = true;
            var config = {
                method: 'POST',
                url: '/api/Account/Login',
                data: {
                    'userName': user.loginUserName,
                    'password': user.loginPassword,
                    'rememberMe': user.loginRememberMe
                }
            };
            $http(config)
                .success(function (data) {
                    authentication.isAuthenticated = true;
                    userService.setUserName(user.loginUserName);
                    localStorage.isAuthenticated = 'yes';
                    $scope.template = $scope.templates[1];
                    $state.transitionTo('home');
                })
        };
    }
]);

Other Controller, that retrieves data

app.controller('BarController', ['$scope', 'userService', function($scope, userService) {
    var username = userService.getUser().name
});

Since your retrieval is async, you might need to add some event/callback mechanism to your user service, so the controllers get notified when the name is set / changes.

Upvotes: 1

Related Questions