thardy / generator-ngbp

Yeoman generator based on the ngBoilerplate kickstarter, a best-practice boilerplate for any scale Angular project built on a highly modular, folder-by-feature structure.
100 stars 23 forks source link

PageTitle binding does not work #25

Open everson opened 9 years ago

everson commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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

jluna79 commented 9 years ago

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