19

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)?

4
  • 1
    Is it working when you're not using a service but filling $scope.content manually ? Commented Jan 21, 2014 at 12:37
  • 1
    ... yes. So it must be something to do with the promise object? Commented Jan 21, 2014 at 12:40
  • if someone has a practical solution please post it here :stackoverflow.com/questions/29436735/… Commented Apr 4, 2015 at 18:03
  • <ion-view> <ion-nav-title>{{ result.title }}</ion-nav-title> Commented Apr 20, 2017 at 7:21

12 Answers 12

80

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works in my case where I set the title after calling an external api, and it's much easier than other options I have found.
11

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>

Comments

10

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

Comments

7

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.

Comments

4

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.

Comments

4

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>

Comments

3

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.

2 Comments

This does not work for me, $ionicNavBarDelegate.title() still does not update my title and I have tried both title and view-title attribute with no success
i have the same problem using $ionicNavBarDelegate.title(myTitle) directly from the controller. When I run this it shows the title briefly and a moment later blanks it.
0

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

Comments

0

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

Comments

0

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/

Comments

0

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>

Comments

0
<ion-view> <ion-nav-title>{{ result.title }}</ion-nav-title>

This work for me

Comments

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.