1

I'm trying to figure out why a script I'm using is giving an error. I have two pages on my application and I would like some header text to change when the view is switched. It works in its current form, but I keep getting a console error which I'd like to fix. Here is the code:

.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
        $rootScope.title = current.$$route.title;
    });

And then in my HTML:

<h1 class="section-intro" ng-bind="title"></h1>

But I keep getting this error:

TypeError: Cannot read property 'title' of undefined
    at app.js:39
    at Scope.$broadcast (angular.js:16165)
    at angular-route.js:619
    at processQueue (angular.js:14567)
    at angular.js:14583
    at Scope.$eval (angular.js:15846)
    at Scope.$digest (angular.js:15657)
    at Scope.$apply (angular.js:15951)
    at HTMLBodyElement.<anonymous> (angular.js:12086)
    at HTMLBodyElement.jQuery.event.dispatch (jquery.js:4435)

Which has something to do with:

   $rootScope.title = current.$$route.title;

I'm mostly confused because the script works, but the error is being thrown when I switched back and fourth.

2
  • In your view it'd be $root.title not just title. Also, double $$ between current and route? $$ is reserved for private variables. Commented Aug 21, 2015 at 21:19
  • How do you define title? Commented Aug 21, 2015 at 21:31

1 Answer 1

1

It's going to be simple fix, just use current.title instead of private $$route object. current route is going to inherit title:

app.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
        $rootScope.title = current.title;
    });
}]);
Sign up to request clarification or add additional context in comments.

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.