generator-ngbp icon indicating copy to clipboard operation
generator-ngbp copied to clipboard

PageTitle binding does not work

Open everson opened this issue 9 years ago • 4 comments

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.

everson avatar Jun 12 '15 14:06 everson

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 avatar Jun 12 '15 17:06 thardy

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>

thardy avatar Jun 12 '15 17:06 thardy

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

jluna79 avatar Jun 16 '15 15:06 jluna79

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;
            }
        }
    });

jluna79 avatar Jun 16 '15 15:06 jluna79