Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle re-assignment of dropdown items #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aantillonl
Copy link

@aantillonl aantillonl commented Apr 11, 2017

Hi, first of all very nice directive, i've used it in my project and love how it works.

would you consider this pull request based in the following issue?

I found that when the object binded to the dropdown items is re-assigned, the dropdown items are not updated.

for example, if we set the dropdown items in the controller, and then that value is re-assigned later in the app, for example

// Assgin the dropdown items
self.defaultDropdownStrings = ['China', 'Sweden', 'United Kingdom', 'United States'];

// Re-assgin after 10 seconds. Dropdown not updated
setTimeout(function(){  
    self.defaultDropdownStrings = ['China', 'Sweden', 'United Kingdom', 'United States', 'Mexico']; 
    
    // Although this would work
    // scope.defaultDropdownStrings.push('Mexico');
}, 10000) 

See it in these plunkers (which i forked from yours, so they will look familiar :p ), I am re-assigning the items in line 100

I believe this is because once it is assigned, the binding in the directive is made to that object (called scope.defaultDropdownItems in the directive), and then the variable scope.dropdownItems is set to the value of scope.defaultDropdownItems. But then, if the list is re-assigned, scope.defaultDropdownItems, will change, but not scope.dropdownItems because the directive is only making the assignment once at the beginning, and is only "watching" for scope.dropdownItems.

I have worked around this by adding a $watch to the directive code, to watch for changes in the variable scope.defaultDropdownItems, and re-assign the value of scope.dropdownItems when scope.defaultDropdownItems changes.

Additionally I've separated the controller function because in my app i had to $inject the dependencies explicitly, but this is not really important.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant