How to set page title with AngularJS -
this question has answer here:
i using following code display page titles each of angularjs app template, whenever try enter invalid url test .otherwise following error:
typeerror: cannot read property 'title' of undefined @ http://localhost/app/js/app.js:34:43 below app.js, index.html code used:
app.js: var myapp = angular.module('myapp', ['ngroute', 'ngsanitize']); myapp.config(['$routeprovider', function($routeprovider) { $routeprovider.when('/home', { templateurl: 'templates/index.html', controller: 'maincontroller', title: 'welcome home page' }); $routeprovider.otherwise({ redirectto: '/home' }); }]); myapp.run(['$location', '$rootscope', function($location, $rootscope) { $rootscope.$on('$routechangesuccess', function (event, current, previous) { $rootscope.title = current.$$route.title; }); }]); index.html:
<title ng-bind="title +' - myapp home'"> - myapp</title> please suggest
the $routechangesuccess event triggered no matter what, avoid error testing existence of route:
myapp.run(['$location', '$rootscope', function($location, $rootscope) { $rootscope.$on('$routechangesuccess', function (event, current, previous) { if (current.hasownproperty('$$route')) { $rootscope.title = current.$$route.title; } }); }]);
Comments
Post a Comment