Javascript AngularJS select2不显示任何值
我正在按中指定的方式使用select2Javascript AngularJS select2不显示任何值,javascript,jquery,angularjs,Javascript,Jquery,Angularjs,我正在按中指定的方式使用select2 我认为您必须像这样使用$timeout: function sampleController($scope, $timeout){ $scope.select2=""; $scope.range=[{text:"name1", value:"id1"}, {text:"name2", value:"id2"}, {text:"name3", value:"id3"}]; $timeout(functi
我认为您必须像这样使用$timeout:
function sampleController($scope, $timeout){
$scope.select2="";
$scope.range=[{text:"name1", value:"id1"},
{text:"name2", value:"id2"},
{text:"name3", value:"id3"}];
$timeout(function(){
$("#e1").select2();
}, 0);
}
为什么??因为$timeout将在执行函数之前等待$digest的结束(在您的示例中为ng repeat)
您以前的代码执行以下操作:
- 呈现HTML
- $(“#e1”)。选择2()李>
- 解释ng重复
查看$timeout文档:$timeout您显示的代码在我看来很好。您能包括您拥有
$scope.range
的型号吗?另外,您是否正确地包括了ng控制器?请检查我的回答是否对您有帮助!
<div ng-controller="sampleController">
<select ui-select2 ng-model="select2" data-placeholder="Pick a number">
<option value="">value1</option>
<option value="">value2</option>
<option value="">value3</option>
</select>
</div>
function sampleController($scope){
$scope.select2="";
$scope.range=[{text:"name1", value:"id1"},
{text:"name2", value:"id2"},
{text:"name3", value:"id3"}];
$("#e1").select2();
}
function sampleController($scope, $timeout){
$scope.select2="";
$scope.range=[{text:"name1", value:"id1"},
{text:"name2", value:"id2"},
{text:"name3", value:"id3"}];
$timeout(function(){
$("#e1").select2();
}, 0);
}