Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS select2不显示任何值_Javascript_Jquery_Angularjs - Fatal编程技术网

Javascript AngularJS select2不显示任何值

Javascript 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

我正在按中指定的方式使用select2


我认为您必须像这样使用$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重复
因此,select2函数在select具有选项元素之前执行


查看$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);
}