AngularJS - (using Ionic framework) - data binding on header title not working

Problem

I'm using an AngularJS-based library called "Ionic" (http://ionicframework.com/).

This seems simple, but it isn't working for me.

In one of my views, I have the following

<view title="content.title">
  <content has-header="true" padding="true">
    <p>{{ content.description }}</p>
    <p><a class="button button-small icon ion-arrow-left-b" href="#/tab/pets"> Back to home</a></p>
  </content>
</view>

In the controller for the above view, I have

angular.module('App', []).controller('DetailCtrl', function($scope, $stateParams, MyService) {

  MyService.get($stateParams.petId).then(function(content) {
    $scope.content = content[0];
    console.log($scope.content.title); // this works!
  });
});

The data for this view is loaded via a simple HTTP GET service (called MyService).

The problem is that when I view this page,

<view title="content.title">

Doesn't display the title. It's just a blank. According to the Ionic documentation (http://ionicframework.com/docs/angularjs/controllers/view-state/), I think I'm doing the right thing.

It's strange that "{{content.description}}" part works, but "content.title" doesn't work?

Also, is it because I'm loading the content dynamically (via HTTP GET)?

Problem courtesy of: ericbae

Solution

Here's a working example of how to accomplish this in Ionic. Open the menu, then click "About". When the "About" page transitions, you will see the title that was resolved.

As Florian noted, you need to use a service and resolve to get the desired effect. You then inject the returned result into the controller. There are some down sides to this. The state provider will not change the route until the promise is resolved. This means there may be a noticeable lag in the time the user tries to change location and the time it actually occurs.

http://plnkr.co/edit/p9b6SWZmBKWYm0FIKsXY?p=preview

Solution courtesy of: Justin

Discussion

I was encountering the same problem and was able to solve it by wrapping my title in double-curlies.

<ion-view title="{{ page.title }}">

I should note that my page.title is being set statically by my controller rather than from a promise.

Discussion courtesy of: plong0

Use ion-nav-title instead of the directive view-title.
see http://ionicframework.com/docs/api/directive/ionNavTitle/

Discussion courtesy of: Calcal Kcwkcw

I got this to work on older versions of Ionic using the <ion-view title={{myTitle}}> solution (as per plong0's answer).

I had to change to <ion-view view-title= in the more recent versions. However using beta-14 it's showing blank titles again.

The nearest I've got to a solution is to use $ionicNavBarDelegate.title(myTitle) directly from the controller. When I run this it shows the title briefly and a moment later blanks it.

Very frustrating.

Discussion courtesy of: Kevin Gurden

If you look at ionic view directive source on github, it's not watching on title attributes which means it won't update your view when you set a new value.

The directive is processed before you receive the answer from server and you fill $scope.content.title.

You should indeed use a promise in your service and call it in a resolver. That or submit a pull request to ionic.

Discussion courtesy of: Florian F.

A solution for newer versions of Ionic is to use the <ion-nav-title> element rather than the view-title property. Just bind your dynamic title inside the content of the <ion-nav-title> using curly brace syntax. Example:

<ion-view>
    <ion-nav-title>
      {{myViewTitle}}
    </ion-nav-title>
  <ion-content>
    <!-- content -->
  </ion-content>
</ion-view>
Discussion courtesy of: cmrn

I had a very similar issue where the title wouldn't update until i switched pages a couple of times. If i bound the title another place inside the page, it would update right away. I finally found in the ionic docs that parts of those pages are cached. This is described here http://ionicframework.com/docs/api/directive/ionNavView/

To solve my issue, I turned caching off for the view with the dynamic title:

<ion-view cache-view="false" view-title="{{title}}">
...
</ion-view>
Discussion courtesy of: fantapop

By using the ion-nav-title directive (available since Ionic beta 14), the binding seems to work correctly.

Rather than

<ion-view title="{{content.title}}">
    ....

Do this

<ion-view>
    <ion-nav-title>{{content.title}}</ion-nav-title>
    ...

Works a treat.

Discussion courtesy of: Dunc

This is the true solution: data bind the ion-nav-title directive

<ion-view>
  <ion-nav-title ng-bind="content.title"></ion-nav-title>
  <ion-content has-header="true" padding="true">
    <p>{{ content.description }}</p>
    <p><a class="button button-small icon ion-arrow-left-b" href="#/tab/pets"> Back to home</a></p>
  </ion-content>
</ion-view>

http://ionicframework.com/docs/api/directive/ionNavTitle/

Discussion courtesy of: TaeKwonJoe
<ion-view> <ion-nav-title>{{ result.title }}</ion-nav-title>

This work for me

Discussion courtesy of: Gautam Menariya

It's the first time that I worked with dynamic title in Ionic 1.7 and I run into this problem. So I solved using $ionicNavBarDelegate.title(') from the controller, as mentioned Kevin Gurden. But additionally, I used cache-view="false".

View:

<ion-view cache-view="false"></ion-view>

Controller:

angular
  .module('app', [])
  .controller('DemoCtrl', DemoCtrl);

  DemoCtrl.$inject = ['$ionicNavBarDelegate'];

  function DemoCtrl($ionicNavBarDelegate) {
    $ionicNavBarDelegate.title('Demo View');
  }
Discussion courtesy of: BorisGastelu

I m using ionic v1.3.3 with side menus based template. I tried all solutions given above but no luck. I used the delegate from $ionicNavBarDelegate: http://ionicframework.com/docs/v1/api/service/$ionicNavBarDelegate/

I created a function inside my angular controller to set the title :

angular.module('app.controllers').controller('contributionsCtrl',  contributionsCtrl);

function contributionsCtrl($scope, $ionicNavBarDelegate) {

    vm.setNavTitle = setNavTitle;


    function setNavTitle() {
        var title = "<span class='smc_color'> <i class='icon ion-images'></i> Your Title </span>"
        $ionicNavBarDelegate.title(title);
    }
}

Then inside my html just called the function vm.setNavTitle

 <ion-view overflow-scroll=true ng-init="vm.setNavTitle()">
    <ion-content></ion-content>
 </ion-view>
Discussion courtesy of: Abba

This recipe can be found in it's original form on Stack Over Flow.