Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageTitle binding does not work #25

Open
everson opened this issue Jun 12, 2015 · 4 comments
Open

PageTitle binding does not work #25

everson opened this issue Jun 12, 2015 · 4 comments

Comments

@everson
Copy link
Contributor

everson commented Jun 12, 2015

Do I need to do something special to make it work? the code generated suggests that each route can set the title, but that does not work as it is. Help here would be appreciated.

@thardy
Copy link
Owner

thardy commented Jun 12, 2015

That must have gotten refactored out accidentally at some point. I'll add it back in soon, but the code you need is in app.js. Just replace the current empty AppController with the following.

app.controller('AppController', function ($scope, $state) {
        $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
            if (angular.isDefined(toState.data.pageTitle)) {
                $scope.pageTitle = toState.data.pageTitle;
            }
        });
    });

It just plugs into a router event to get the route data and place it on the scope.

@thardy
Copy link
Owner

thardy commented Jun 12, 2015

I'm probably going to change it to the following to be consistent with the "controller as" syntax when I put it in the generator...

app.controller('AppController', function ($scope, $state) {
        var model = this;

        $scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
            if (angular.isDefined(toState.data.pageTitle)) {
                model.pageTitle = toState.data.pageTitle;
            }
        });
    });

and change index.html to ...

<html ng-app="<%=appName%>" ng-controller="AppController as model">
<head>
    <title ng-bind="model.pageTitle"></title>

@jluna79
Copy link

jluna79 commented Jun 16, 2015

Thanks! 👍 (I noticed it wasn't working and thought I had modified something that broke it ).

@jluna79
Copy link

jluna79 commented Jun 16, 2015

BTW, may I suggest the following changes to the above code to go in accordance with the Angular styleguide?

app.controller('AppController', function ($scope, $state) {
        var model = this;

        $scope.$on('$stateChangeSuccess', stateChangeSuccess);

        function stateChangeSuccess(event, toState, toParams, fromState, fromParams) {
            if (angular.isDefined(toState.data.pageTitle)) {
                model.pageTitle = toState.data.pageTitle;
            }
        }
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants