2

In my Angular 1.5 app (a SPA), I use user profile information on every "page" of the app. So, in my app.js file, I include the following code:

$http.get(API_URL + "/user")
            .then(function success(response){
                $rootScope.userInfo = response.data;
            },
            function error(response){

            });

And then in each component's controller and template, I make reference to this object like the following:

<img class="profile icon" ng-src="{{$rootScope.userInfo.profile_photo}}">

However, the image never shows up. I assume because the callback setting the $rootScope.userInfo from the response is called after the template has been loaded. How do I ensure it gets updated when the response comes in from the GET call?

EDIT - Here's more info, since the answers coming in about "don't use $rootScope in the view" isn't working for me. Making the changes suggested doesn't work. Do I need to reference $ctrl in this case?

angular.module('xxx')
        .component("navBar", {
            templateUrl: "/components/navBar/navBar.html",
            controller: NavBarController,
            bindings: {

            }
        });

function NavBarController($rootScope, $scope, $uibModal, $timeout, $http, API_URL) {
        var vm = this;
2
  • I believe you do NOT have to prefix the path with $rootScope, i.e. try ng-src="{{userInfo.profile_photo}}". Commented Oct 31, 2016 at 13:14
  • scopes are always isolate in a component. see docs.angularjs.org/guide/component#!. Therefore, components do not inherit from or have access to $rootScope. Using $rootScope is considered by many to be an anti-pattern anyway; It is recommended to use a service to store persistent data, and a requirement to use a service when in combination with components. While you might be able to use $root to get around this restriction, this is not recommended behavior. Commented Oct 31, 2016 at 13:34

3 Answers 3

2

Remember that the view is bound to the $scope already, you never write the following

<img class="profile icon" ng-src="{{$scope.userInfo.profile_photo}}">

instead

<img class="profile icon" ng-src="{{userInfo.profile_photo}}">

All scopes, except isolate scopes, have inheritance and the $rootScope is at the top of that chain, so as long as no other $scope have a userInfo property in that chain writting

<img class="profile icon" ng-src="{{userInfo.profile_photo}}">

will point to the property in the $rootScope

If you are inside a isolate scope you could write

<img class="profile icon" ng-src="{{$root.userInfo.profile_photo}}">

since all scopes, even isolated, have a $root property that points to the $rootScope

Check here

angular.module('app', [])
  .controller('TestCtrl', function($scope) {
    // An empty controller to create a new scope
    // Only for demonstration of inheritance 
  })
  .directive('directive', function() {
    return {
      restrict: 'E',
      scope: {},
      template: '<div>{{$root.userInfo.name}}</div>'
    };
  })
  .run(function($rootScope) {
    $rootScope.userInfo = {
      name: 'John'
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div>{{userInfo.name}}</div>
  <div ng-controller="TestCtrl">
    {{userInfo.name}}
  </div>
  <directive></directive>
</div>

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

2 Comments

as I mention in my comment, components are always isolate, and $root may be necessary, but not recommended for this situation.
@Claies I'm aware, but as long as the OP does not change how his app is designed this is a working solution for him. You don't know for sure the cost of the change and even if he changes how that works, then it will be subject for another question. For his present problem this is the solution.
2

Just use,

<img class="profile icon" ng-src="{{userInfo.profile_photo}}">

DEMO

1 Comment

Please see my edits. This solution didn't work, probably because of how I set up the component
0

you dont have to put $rootScope in view you should put like this

<img class="profile icon" ng-src="{{userInfo.profile_photo}}">

5 Comments

What is different from the answer i gave?
@Sajeetharan that he posted it 2 seconds after you and didn't see your answer
Not 2 secs actually 1 minute
@Sajeetharan, site didnt updated till i post my answer.So i didnt see your answer.Good catch friend
@Nikhil it happens,but i thought your answer is different

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.