使用angularjs在Select2中设置所选值

使用angularjs在Select2中设置所选值,angularjs,jquery-select2-4,ui-select2,angularjs-select2,Angularjs,Jquery Select2 4,Ui Select2,Angularjs Select2,我的Anjularjs应用程序中有一个下拉列表,使用“ui.select2”实现 我已将其初始化如下 <input type="text" ng-model="objCas.iProjectId" id=" iprojectid" ui-select2="iProjectIdOption" /> HTML代码:不需要id js代码: 仅将此选项用于默认装箱 $Scope.objCas.iprojectd={“text”:“2010/256-/dummytext”,“id”:24

我的Anjularjs应用程序中有一个下拉列表,使用“ui.select2”实现

我已将其初始化如下

  <input type="text" ng-model="objCas.iProjectId" id=" iprojectid" ui-select2="iProjectIdOption" />
HTML代码:不需要id
js代码:
仅将此选项用于默认装箱
$Scope.objCas.iprojectd={“text”:“2010/256-/dummytext”,“id”:240}
示例代码:
链接:http://embed.plnkr.co/K66Pf0/
替换script.js文件:
//代码在这里
var myAppModule=angular.module('MyApp',['ui.select2']);
myAppModule.controller('MyController',函数($scope){
$scope.select2选项={
initSelection:函数(元素,回调){
},
阿贾克斯:{
url:“./data.json”,
数据:功能(术语,第页){
return{};//查询参数转到此处
},
结果:函数(数据,页面){//将结果解析为Select2所需的格式。
//因为我们使用的是自定义格式函数,所以不需要更改远程JSON数据
返回{results:data}
}
}
}
log($scope.select2Options-“,$scope.select2Options.initSelection);
$scope.testModel={“id”:4,“text”:“Fourth”,“color”:“red”}
;
});
您可以在下拉列表中查看默认选定值。

获取select2元素对象并应用以下代码:

angular.element("#select2_id").select2('data', { "text": "text", "id": [id]});

像个魔术师一样工作。。。谢谢
    $Scope.objCas.iProjectId= {"text":"2010 / 256 - / dummytext","id":240}    
    $Scope.objCas.iProjectId=2;
    $scope.iProjectId.selected = {"text":"2010 / 256 - / dummytext","id":240}
    HTML code: no need of id
    <input type="text" ng-model="objCas.iProjectId" ui-select2="iProjectIdOption" />

    js code:
    use only this for default biniding
    $Scope.objCas.iProjectId= {"text":"2010 / 256 - / dummytext","id":240}  


    example code:
    link: http://embed.plnkr.co/K66Pf0/
    replace script.js file:
    // Code goes here
var myAppModule = angular.module('MyApp', ['ui.select2']);

myAppModule.controller('MyController', function($scope) {
    $scope.select2Options = {
      initSelection: function (element, callback) {

                },
      ajax: {
        url: "./data.json",
        data: function (term, page) {

          return {}; // query params go here
        },
        results: function (data, page) { // parse the results into the format expected by Select2.
          // since we are using custom formatting functions we do not need to alter remote JSON data
          return {results: data}
        }
      }
    }
    console.log("$scope.select2Options-",$scope.select2Options.initSelection);
    $scope.testModel={ "id": 4, "text": "Fourth", "color": "red" }
;
});

    you can view default selected value in dropdown.
angular.element("#select2_id").select2('data', { "text": "text", "id": [id]});