Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
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 4 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. –  Matthew Canty Jun 4 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 4 at 10:35
 
Thanks, updated again. –  Matthew Canty Jun 4 at 10:36

4 Answers

up vote 0 down vote accepted

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.

share|improve this answer
 
Could you elaborate on why the names and surnames need to be stored using a hash? –  callmekatootie Jun 4 at 10:46
 
You are a life-saver. –  Matthew Canty Jun 4 at 10:49
 
New plunkr. You can just remove the old name+surname from the list when old was present. plnkr.co/edit/dChHKr?p=preview –  rewritten Jun 4 at 10:54
 
Is there any way to get a count from this? –  Matthew Canty Jun 4 at 12:42
 
How to get a count - stackoverflow.com/a/6700/966609 –  Matthew Canty Jun 5 at 10:31
show 1 more comments

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

share|improve this answer
 
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. –  Matthew Canty Jun 4 at 12:46
 
HOpe you don't mind. Had to go and chuck away all those methods - plnkr.co/edit/yltBop?p=preview –  Matthew Canty Jun 4 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. –  Olivér Kovács Jun 4 at 12:57
 
Good point. Thanks. –  Matthew Canty Jun 4 at 13:39

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

share|improve this answer
 
Sorry the Plunker link was incorrect please can you look again. Also I have edited my question slightly. –  Matthew Canty Jun 4 at 10:33

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.

share|improve this answer
 
Go back and change a name again and it doesn't get replaced. Sorry I should have stated that. –  Matthew Canty Jun 4 at 10:45
 
@MatthewCanty Corrected the answer. Is this the functionality you are looking for? –  callmekatootie Jun 4 at 10:50
 
Not quite rewritten's answer is what I am looking for. See how when you change a surname twice for the same first name, you are still left with one full name. –  Matthew Canty Jun 4 at 10:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.