0

I have looked for the solution in following two links:

how-to-access-rootscope-value-defined-in-one-module-into-another-module

rootscope-variable-exists-but-not-accessible

But still could not find a solution.

I initialize $rootScope.user variable in $scope.login function, which is triggered by some other button click event, of my controller as:

app.controller('LoginFormController',['$rootScope','$location','$log','$scope','userAngService','userBean',function($rootScope,$location,$log,$scope,userAngService,userBean){
    $scope.login = function(val){
        $scope.user = userAngService.login(val).then(function(data){
            $rootScope.user = data;
            $location.path("/home");
        });
    };
 }]);

Redirecting it to /home mapped in app.js as:

    angular.module('Writer', ['AllControllers','ngRoute'])
.config(['$routeProvider', function($routeProvider,$Log){
    $routeProvider.when('/Diary',{
        templateUrl:'login.html',
        controller: 'LoginFormController'
    })
    .when('/home',{
        templateUrl: 'home.html',
        controller: 'ReachController'
     })
    .otherwise({redirectTo : '/Diary'});
}]);

Now I am accessing $rootScope.user variable in the mapped controller ReachController as:

app.controller('ReachController',['$scope','$rootScope',function($scope,$rootScope){
    $scope.user = {};
    $scope.user = $rootScope.user;
    console.log($scope.user);
}]);

It is perfectly logging angular object in console but is not avaiable in my home.html page. Here is the html page - accessing it in <h2> and <h3> tags:

    <div>
        <h2>{{$scope.user}}hhhello</h2>
        <h3>{{$scope.user.author}}</h3>
        <div id="welcomeStory">
            <span id="wsTitleBub">Title</span>
            <input id="wsTitle" type="text" ng-model="wsPublish.welcomeStoryTitle" />{{wsPublish.welcomeStoryTitle}}
            <h6>Words..</h6>
            <textarea id="wsWords" ng-model="wsPublish.welcomeStoryWords"></textarea>
            <input id="wsPublish" type="button" name="wsPublish" value="Publish" ng-click="pub = !pub" />
            <input id="wsAddShelf" type="button" name="wsAddToShelf" value="Add To Shelf" ng-click="addToShelf()" />
        </div>
        <div id="wsPublishTo" ng-show="pub">
            <ul>
                <li>
                    <input type=submit id="wsgroups" value="Group" ng-click="publishGroup(wsPublish)" />
                </li>
                <li>
                    <button id="wsPublic" ng-click="public()">Public</button>
                </li>
            </ul>
        </div>
    </div>

8
  • 1
    I don't think you want $scope in your binds in your markup. Commented Mar 18, 2016 at 4:49
  • 2
    why are you using $scope in view/markup Commented Mar 18, 2016 at 4:50
  • 1
    try changing $scope.user to just user Commented Mar 18, 2016 at 4:51
  • 1
    @HarshvardhanSolanki You know javascript is a bit shy.. the local-scope``variables of a Function are always sort of private, unless you put them in the prototype of the functions by using prototype or this property of the Function. since $scope is such local variable you can't use it directly inside your view, since the view is only handed an instance of your controller function. Commented Mar 18, 2016 at 5:10
  • 1
    @HarshvardhanSolanki you can't do that directly. you'll have to access the property inside the ng-repeat block. like {{author.name}} in your mark up.. on a side note you can create a filter to map the array that property but that would be an overkill and I'll suggest you not to not to do that Commented Mar 18, 2016 at 5:31

2 Answers 2

0

Just change your $scope.user to user in your markup and it'll work.

Sign up to request clarification or add additional context in comments.

Comments

0

Once it is in $rootScope you can directly access it in markup with {{user}} no need to again assign it to scope variable!

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.