0

Is it possible to use rootscope's vriable inside scope? If yes then how?

For example, I want to set value of $rootScope.user_name in sub_menu property of scope.

var app = angular.module('myApp', []).run(function ($rootScope) {$rootScope.user_name = "A to Z Traders";$rootScope.user_role="Admin";});

app.controller('headerCtrl', function($scope) {
    $scope.menu=[
        {menu_name : "Seller's Name", menu_id: "user", sub_menu:[{$rootScope.user_name}, "Profile", "Add Brand Owner", "Logout"]}
    ];

});
AranS
  • 1,871
  • 10
  • 22

1 Answers1

1

Sure you can.

Just inject $rootScope into the contoller ang get value from there

app.controller('headerCtrl', ['$scope', '$rootScope', function($scope, $rootScope) { 
$scope.menu=[ {menu_name : "Seller's Name", menu_id: "user", sub_menu:[{$rootScope.user_name}, "Profile", "Add Brand Owner", "Logout"]} ];
    }]);

But this is better solution to use service, where you will store data and functionality

Also you have to read about scopes inheritance

T J
  • 42,762
  • 13
  • 83
  • 138
Drag13
  • 5,859
  • 1
  • 18
  • 42