0

I have the following problem:

I have a two states, defined as follows:

  state('home', {
        url: '/home',
        templateUrl: 'home/Home.html',
       controller: 'HomeController as hc'
   }).state('home.index', {
       url: '/index',
       templateUrl: 'index/Index.html',
       controller: 'IndexController as ic'
   })

In HomeController, I defined value responsible for my title in navbar, which is stored in a service:

this.title = Service.getTitle();

And this is a title in my navbar. When I change state to Index, I set new title:

this.title = Service.updateTitle("Index")

However, this does not change the title of my navbar.

Service has one variable title, and two functions:

getTitle(){
        return this.title;
    }

updateTitle(newTitle){
        this.title = newTitle;
        return;
    }

Any ideas on what am I doing wrong?

uksz
  • 18,239
  • 30
  • 94
  • 161

3 Answers3

1

Ok, so the answer to this question is little tricky. Binding with a serivce works well if you dont bind it with primitive such as boolean, or string. All I had to do, is change a structure of a title. Ive changed it to object, to that it looks as following:

    title:{
     name: "Name"
    }

And this will work. All you need to know, is that the binding will not update if the data type is a primitive. It has to be object, or you have to set events or a watch for :)

uksz
  • 18,239
  • 30
  • 94
  • 161
0

when you update your title you say this.title = Service.updateTitle("Index");

This way you change the binding because updateTitle returns nothing.

Just say Service.updateTitle("Index") without the lefthand.

  • It was not an issue. The issue is a bit complicated - I've explained it in my own answer – uksz Nov 23 '15 at 09:48
0

If you tried logging the data stored in your service and the data stored is correct when you switch views there must be something wrong on how you update the title in the template when state changes, so have you tried something like broadcasting the event so your navbar directive or controller knows? like:

$rootScope.$broadcast("updateTitleEvent", newTitle);

and catch on your navbar directive or controller

$scope.$on("updateTitleEvent", function (event, newTitle) {
    // Update title goes here
    $scope.title = newTitle;
});

you might want to put your template and controller codes in your post. Hope this helps

masterpreenz
  • 2,280
  • 1
  • 13
  • 21
  • It was not an issue. The issue is a bit complicated - I've explained it in my own answer – uksz Nov 23 '15 at 09:48