Dynamically add options to a Angular chosen combo box

孤者浪人 提交于 2019-12-11 12:30:15

问题


The following code doesn't work:

<select ui-jq='chosen' ng-model='trainer_list' multiple class="form-control" data-placeholder="Select Multiple Trainers" ng-options='trainer.id as trainer.name for trainer in trainers'>

</select>

I took this from an Angular Bootstrap theme, and I'm struggling to use it. Basically I want to use from this angular script

$http({
        method: 'GET',
        url: 'http://localhost/training_system/retrieve_train.php?trainer=Y'
}).success(function (result) {
    $scope.trainers = result;

});

and use it in the combo box, i can't use static options, which apparently are the only ones working. what do i do?

UPDATE: So I tried the solution by Abhilash using ng-repeat instead of ng-options, and it worked the first few attempts. But now I could no longer repeat it, and all I have is an empty dropbox. I didn't change anything, but it's no longer working. Is it possible that ui-jq is incompatible with AngularJS?


回答1:


Try this one:

<select ui-jq='chosen' ng-model='trainer_list' multiple class="form-control" data-placeholder="Select Multiple Trainers">
    <option value="{{trainer.id}}" ng-repeat="trainer in trainers"> {{trainer.name}} </option>
</select>

Debug your variable trainers

Just add this code anywhere in your page to debug,

<label> To Do: Remove this after testing. Trainer = {{trainers}} </label>

And see what are the values in trainer. Is this variable is updating after your server call?




回答2:


This is how I resolved it:

In your select tag, put ng-if="trainers". This would prevent DOM loading until your http request is completed



来源:https://stackoverflow.com/questions/34917098/dynamically-add-options-to-a-angular-chosen-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!