angular-ui-OLDREPO icon indicating copy to clipboard operation
angular-ui-OLDREPO copied to clipboard

Select2 does not data-bind

Open dewang opened this issue 13 years ago • 5 comments

I am trying to create a Select2 with the tags option. Due to the way I have to load data this cannot be done in a declarative way with

<input ui-select2="options" ng-model="selected" />

Instead, I need to do this through Javascript after the data is fetched like so

$('#select-tags').select2(options);

This sets up the input field with tags, but changes are not reflected in the model. The problem can be seen here http://plnkr.co/edit/kuQ3Hehrf0jdWPi4niIb

Angular UI version 0.4.0 (also tried with 0.3.2) Select2 version 3.3.1

dewang avatar Mar 07 '13 11:03 dewang

You didn't provide an example of what data is coming in late. The directive was designed to handle data coming in late. If the issue is the configuration options, you can use ui-if to defer initialization until the configuration options have loaded, however it seems to me that it might not be unreasonable to make the directive handle options being changed asynchronously.

ProLoser avatar Mar 07 '13 11:03 ProLoser

A little bit more digging reveals why I see this behaviour. The call I am making goes directly to Select2 rather than the Angular directive since it does not support being called in this way.

The data coming in late is both, the model as well as the tags to bind to. In my controller I have an AJAX call to fetch a bunch of required data for the page. This data is used to render some select lists as Select2 lists and these work fine. The problem seems to be only in the case of the Select2 with tags option.

So when I have this markup

<input ui-select2="tagOptions" ng-model="TagsString" type="text" />

With this in the Javascript

$scope.tagOptions = { 
    tags: ajaxData.tags || [],
    tokenSeparators: [",", " "],
    width: "200px"
};

The input field is not made in to a Select2 field. I just see a regular text field which does data-bind.

This code http://plnkr.co/edit/vse8tLPD5bmxp39lTOa3 shows what happens when there is a delay before the values are set on scope.

dewang avatar Mar 07 '13 12:03 dewang

AngularUI is currently undergoing a restructure. This project will no longer hold the codebase, can you please relocate this to https://github.com/angular-ui/ui-select2 (after confirming it's still necessary with the latest code)?

ProLoser avatar May 03 '13 09:05 ProLoser

This repo is discontinued. The project has been relocated here: https://github.com/angular-ui/ui-select2

Please reopen this issue there if it is still relevant.

nateabele avatar Sep 09 '13 00:09 nateabele

Was this issue ever recreated on the new repository? If so, can someone provide a link? I'm doing exactly the same thing and struggling with it.

andrewmurray avatar Sep 11 '14 15:09 andrewmurray