1

Please have a look at the following Plunker project.

I want to keep a list of items in a service which multiple controllers can access. In this example when you select a surname it will come up in the list. However if you set a second name the list is cleared. I just cabn't figure out the logic in the $scope.$watch

Users should also be able to select the "please choose" option to remove the name from the list. Then you should see a list of all items which have a last name selected. As in the image the list below should read:

  • Matt Diff
  • Tom Canty

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

I'm still looking at this I just had to abstract it away from my code in case I was doing something wrong there.

enter image description here

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Matt Canty
  • 2,395
  • 5
  • 35
  • 50
  • I am unable to follow what you are expecting here? I selected the surname for the first item. I selected the surname for the second item. What should happen now? – callmekatootie Jun 04 '13 at 10:20
  • Sorry the link may have been wrong. However, if you have selected 2 surnames then there should be a list of 2 full names. – Matt Canty Jun 04 '13 at 10:31
  • 1
    "It should demonstrate exactly what I wish to achieve." Does it mean that that is the expected behavior? What is the expected behavior? (like, "for each of the names, add to the list when the surname has been selected") – rewritten Jun 04 '13 at 10:35

4 Answers4

1

If you watch the service method insted of the ng-model and do all the logic in the service (where it actually belongs), that would be a cleaner way to solve the problem. And faster if you index the items object with the firstName.

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

Oliver
  • 4,471
  • 2
  • 21
  • 18
  • Thanks Olivér. This is a neat solution - I will be implementing it this way. I would use this as the answer however rewritten's really got the core of the issue with the hash. – Matt Canty Jun 04 '13 at 12:46
  • HOpe you don't mind. Had to go and chuck away all those methods - http://plnkr.co/edit/yltBop?p=preview – Matt Canty Jun 04 '13 at 12:52
  • 1
    It's fine as long as you dont want to just add, or just remove an item. In general shorter methods are more reusable, therefore scales better. And more easy to test. – Oliver Jun 04 '13 at 12:57
0

I think your code was write, seems you were missing "$scope." before first_name & was causing error.. now I can see the log getting printed & no errors.

http://plnkr.co/edit/9BvPJh?p=preview

-Bhaskara

0

You should save the names and surnames using a hash. Here there is the edited plunkr:

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

The fact is that you need to trace, for each name, the assigned surname. Otherwise, you are not able to remove it from the list once you assign a new one.

rewritten
  • 16,280
  • 2
  • 47
  • 50
0

Here - this will do the trick for you:

In your `$watch code, replace

listService.removeItem(oldItem);

with

if (newSurname === undefined || newSurname === null || newSurname === "") {
    listService.removeItem(oldItem);
}

Thus, when a surname is selected and it is valid, it is added to the list. When it is reset or not selected (when previously selected), it is removed from the list.

I hope this is what you are looking for.

callmekatootie
  • 10,989
  • 15
  • 69
  • 104