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?

4
  • What are you doing in the Service? Please provide plunkr or a fiddle. Commented Nov 23, 2015 at 9:26
  • @ShashankAgrawal, the service only stores the value of a Title. I will update fiddle/plunkr in a while Commented Nov 23, 2015 at 9:29
  • @uksz there are few links which should also give you some ideas: stackoverflow.com/q/33466662/1679310, stackoverflow.com/a/30213238/1679310 Commented Nov 23, 2015 at 9:42
  • @RadimKöhler, I've actually solved the issue. I will post answer in a second Commented Nov 23, 2015 at 9:46

3 Answers 3

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

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

1 Comment

Good spot ;) yeah, always have dot in the name - stackoverflow.com/q/21882681/1679310
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.

1 Comment

It was not an issue. The issue is a bit complicated - I've explained it in my own answer
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

1 Comment

It was not an issue. The issue is a bit complicated - I've explained it in my own answer

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.