Given the following Angular selec2 controller:
<select ui-select2 id="projectListSelection" data-placeholder="Select a Project ..." ng-model="selectedProject">
@*ng-options="project.WebsiteName for project in projectList"*@
<option value=''></option>
<option ng-repeat="project in projectList" ng-value="'{{ project.WebsiteGUID }}'">{{project.WebsiteName}}</option>
</select>
Model data is retrieved as follows from the controller:
$scope.projectList = dataFactory.getList("") // no parameters
.success(function (result) {
$scope.projectList = result.value;
$("#projectListSelection").select2({ width: '240' });
})
.error(function (error) {
customUIFunctions.showError("Data Error - Unable to Load Project List"); // use default error message
console.log("data error");
});
Here's the issue:
select
drop-down controlselect2
control, however its width is only as small as the placeholderWhen I try setting the width before the data is retrieved (before $scope.projectList = ...
) I get the following errors:
query function not defined for Select2 s2id_projectListSelection ... angular.js:9402 RangeError: Maximum call stack size exceeded ...
Which makes sense as the DOM isn't fully loaded. So...
$(document).ready(function () {
$("#projectListSelection").select2({ width: '240' });
});
However I still see the same issue, where the width conforms first to the placeholder, then to the wider setting once data is loaded.
I managed to get around this doing:
<select ui-select2 id="projectListSelection" style="width:240px;" . . .
However, that's kind of ugly.
The questions:
placeholder
with the initial value of "Loading data..." then watch for your data to load and change scope.placholder to "Select a Project...". Then on your element use placeholder="{{ placeholder }}"