1

I am trying to add viewTitle to base view or root web app page, e.g.

Root Page

 <div ng-app="app"> 
     {{viewTitle}}
     <div ui-view=""></div>
 </div> 

can I change viewTitle in controllers ?

Controller-1

var myApp = angular.module(app, []);
myApp.controller('ChildController', ['$scope', function($scope) {
  // how to update viewTitle here ?
}]);
2

2 Answers 2

2

One solution may be this: If you use ui-router you can add a title in state: (I use this to translate the title)

.state('login', {
            url: '/login',
            controller: 'AdminLoginController',
            templateUrl: 'app/admin/views/login.html',
            title: {
                'es': 'Iniciar sesión',
                'en': 'Login',
                'de': 'Einloggen'
            }
        })
.state('panelAdmin', {
            url: '',
            controller: 'AdminHomeController',
            templateUrl: 'app/admin/views/panelAdmin.html',
            title: {
                'es': 'Panel de administración',
                'en': 'Control panel',
                'de': 'Führungspanel'
            }
        })

And in $stateChangeStart reload the title:

$rootScope.$on("$stateChangeStart", function (event, toState, toParams, fromState, fromParams) {

  if (toState.title) {
    $rootScope.title = toState.title[$rootScope.cultureLang];
  }

});

In index.html:

<title>{{title}}</title>
Sign up to request clarification or add additional context in comments.

Comments

0

I think we can use $rootScope for this purpose. Please see below.

html template

<body ng-app="myApp" >  
        {{viewTitle}}
        <div ui-view=""></div>
    </div> 
</body>

js file

myApp.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/home');

$stateProvider
    .state('home', {
        url: '/',
        template: '<h1>From nested view </h1>',
        controller: function($rootScope)
        {
            $rootScope.viewTitle = "home";
        }
    });

});

Hope it helps

1 Comment

I tried this, don't think it'll work right.

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.