How to manipulate the of an AngularJS single page app?</a></h1> </div> <div class="grid fw-wrap pb8 mb16 bb bc-black-075"> <div class="grid--cell ws-nowrap mr16 mb8" title="2016-01-12 19:07:53Z"> <span class="fc-light mr2">Asked</span> <time itemprop="dateCreated" datetime="2016-11-04T04:38:12.690" class="fromnow">Nov 04 '16 at 04:38</time> </div> <div class="grid--cell ws-nowrap mr16 mb8"> <span class="fc-light mr2">Active</span> <time class="fromnow" title="2016-11-20T20:16:23.323" datetime="2016-11-20T20:16:23.323">Nov 20 '16 at 20:16</a> </div> <div class="grid--cell ws-nowrap mb8" title="Viewed 595 times"> <span class="fc-light mr2">Viewed</span> 595 times </div> </div> <div id="mainbar" role="main" aria-label="questions and answers"> <div id="question" class="question" data-questionid="40415376" data-ownerid="1742777" data-score="1"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40415376"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> <button class="js-bookmark-btn s-btn s-btn__unset c-pointer py4"> <svg aria-hidden="true" class="svg-icon iconBookmark" width="18" height="18" viewBox="0 0 18 18"><path d="M6 1a2 2 0 00-2 2v14l5-4 5 4V3a2 2 0 00-2-2H6zm3.9 3.83h2.9l-2.35 1.7.9 2.77L9 7.59l-2.35 1.7.9-2.76-2.35-1.7h2.9L9 2.06l.9 2.77z"></path></svg> <div class="js-bookmark-count mt4" data-value=""></div> </button> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>I have a single-page AngularJS app. The <code>index.html</code> file looks like this:</p> <pre><code><html ng-app="myApp" ng-controller="MyCtrl as myctrl"> <head> <link rel="stylesheet" href="my-style-sheet.css"> <title>{{myctrl.title}}</title> </head> <body> <div class="container"> <ol> <li><a ui-sref="stateA">StateA</a></li> <li><a ui-sref="stateB">StateB</a></li> </ol> <div ui-view></div> </div> <script src="my-app.js"></script> </body> </html> </code></pre> <p>As the user clicks on the StateA or StateB links, the page displays the content of those pages in place of <code><div ui-view></div></code>. Terrific.</p> <p>As the user clicks around, the displayed content changes. I need the title of the page to change too. Currently it gets the title from the controller <code>MyCtrl</code> like this: <code><title>{{myctrl.title}}</title></code>. But when the user clicks those links, those states each have their own controllers. So I cannot use <code><title>{{myctrl.title}}</title></code>. </p> <p>How can I update the title when various states the user clicks on have different controllers?</p></div> <div class="mt24 mb12"> <div class="post-taglist grid gs4 gsy fd-column"> <div class="grid ps-relative"> <a href="../../questions/tagged/javascript" class="post-tag js-gps-track" title="show questions tagged 'javascript'" rel="tag">javascript</a> <a href="../../questions/tagged/html" class="post-tag js-gps-track" title="show questions tagged 'html'" rel="tag">html</a> <a href="../../questions/tagged/angularjs" class="post-tag js-gps-track" title="show questions tagged 'angularjs'" rel="tag">angularjs</a> <a href="../../questions/tagged/angularjs-controller" class="post-tag js-gps-track" title="show questions tagged 'angularjs-controller'" rel="tag">angularjs-controller</a> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="edited Nov 20 '16 at 20:16">edited Nov 20 '16 at 20:16</time> <a href="../../users/1016716/mr-lister" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1016716.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Mr Lister" /> </a> <div class="s-user-card--info"> <a href="../../users/1016716/mr-lister" class="s-user-card--link">Mr Lister</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">45,515</li> <li class="s-award-bling s-award-bling__gold" title="15 gold badges">15</li> <li class="s-award-bling s-award-bling__silver" title="108 silver badges">108</li> <li class="s-award-bling s-award-bling__bronze" title="150 bronze badges">150</li> </ul> </div> </div> </div> <div class="post-signature owner grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Nov 04 '16 at 04:38">asked Nov 04 '16 at 04:38</time> <a href="../../users/1742777/saqib-ali" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1742777.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Saqib Ali" /> </a> <div class="s-user-card--info"> <a href="../../users/1742777/saqib-ali" class="s-user-card--link">Saqib Ali</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">11,931</li> <li class="s-award-bling s-award-bling__gold" title="41 gold badges">41</li> <li class="s-award-bling s-award-bling__silver" title="133 silver badges">133</li> <li class="s-award-bling s-award-bling__bronze" title="272 bronze badges">272</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-40415376" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="40415376" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-68080638" class="comment js-comment " data-comment-id="68080638" data-comment-owner-id="4702544" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment68080638_40415376"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">check this http://stackoverflow.com/questions/23813599/set-page-title-using-ui-router</span> – <a href="../../users/4702544/sachin-gupta" title="7,805 reputation" class="comment-user ">Sachin Gupta</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/40415376/how-to-manipulate-the-title-of-an-angularjs-single-page-app#comment68080638_40415376"><span title="2016-11-04T04:53:03.537 License: CC BY-SA 3.0" class="relativetime-clean">Nov 04 '16 at 04:53</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <div id="answers"> <a name="tab-top"></a> <div id="answers-header"> <div class="answers-subheader grid ai-center mb8"> <div class="grid--cell fl1"> <h2 class="mb0" data-answercount="9">2 Answers<span style="display:none;" itemprop="answerCount">2</span></h2> </div> </div> </div> <a name="40415447"></a> <div id="answer-40415447" class="answer " data-answerid="40415447" data-ownerid="1317053" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40415447"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>You can attach some data to each state of your routes, like a value that can be used to set the title of the page.</p> <pre><code>.state('test', { url: '/test', templateUrl: '/templates/test.html', data: { title: 'test title' } }) </code></pre> <p>Then write a directive to read the title. You can check to see if the required data is available on the state you are going to, and attach the whole thing to <code>$stateChangeSuccess</code> event.</p> <pre><code>function dynamicTitle($rootScope, $timeout) { return { link: function(scope, el) { $rootScope.$on('$stateChangeSuccess', function(event, toState) { var title = (toState.data && toState.data.title) ? toState.data.title : 'Default title'; $timeout(function() { el.text(title); }, 0, false); };); } }; } angular.module('myApp').directive('dynamicTitle', dynamicTitle); </code></pre> <p>And attach it to your <code><title></code></p> <pre><code><title dynamic-title></title> </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2016-11-04T04:50:47.713" class="relativetime">Nov 04 '16 at 04:50</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Aᴍɪʀ</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 04 '16 at 04:45">answered Nov 04 '16 at 04:45</time> <a href="../../users/1317053/a-ir" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1317053.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Aᴍɪʀ" /> </a> <div class="s-user-card--info"> <a href="../../users/1317053/a-ir" class="s-user-card--link">Aᴍɪʀ</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">7,623</li> <li class="s-award-bling s-award-bling__gold" title="3 gold badges">3</li> <li class="s-award-bling s-award-bling__silver" title="38 silver badges">38</li> <li class="s-award-bling s-award-bling__bronze" title="52 bronze badges">52</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="40415535"></a> <div id="answer-40415535" class="answer " data-answerid="40415535" data-ownerid="4593781" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="40415535"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>You can create an AngularJS factory, inject it, modify it by calling 'Title.setTitle()' from controllers</p> <pre><code> <html ng-app="app" ng-controller="Ctrl"> <head> <title>{{ Title.title() }}</title> app.factory('Title', function() { var title = 'Hello'; return { title: function() { return title; }, setTitle: function(newTitle) { title = newTitle } }; }); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Nov 04 '16 at 04:54">answered Nov 04 '16 at 04:54</time> <a href="../../users/4593781/muhammad-faizan-uddin" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/4593781.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Muhammad Faizan Uddin" /> </a> <div class="s-user-card--info"> <a href="../../users/4593781/muhammad-faizan-uddin" class="s-user-card--link">Muhammad Faizan Uddin</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">1,339</li> <li class="s-award-bling s-award-bling__silver" title="12 silver badges">12</li> <li class="s-award-bling s-award-bling__bronze" title="29 bronze badges">29</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>