2014-08-28 64 views
0

我試圖設置select2下拉菜單的初始值。問題是值是一個對象。 我試着搞initSelection,但我甚至不能打電話給它。我把debugger放在裏面,沒有任何反應。 ngSelected不工作,要麼:(如何在對象上使用ngRepeat時設置選擇

我做了一個plunker

標記

<select ui-select2="opt" 
     ng-change="save()" 
     ng-model="chosen" 
     data-placeholder="Choose"> 
     <option value=""></option> 
     <option ng-repeat="item in list" value="{{item}}">{{item.a + ' ' + item.b}}</option> 
</select> 

控制器

app.controller('MainCtrl', function($scope) { 
    $scope.selected = {a: 2, b: 22}; 
    $scope.list = [{a: 1, b: 11}, {a: 2, b: 22}]; 

    $scope.opt = { 
    allowClear: true, 
    initSelection: function (elem, callback) { 
     debugger; // this never triggers... :(
    } 
    }; 

    $scope.save = function() { 
    $scope.pre = $scope.chosen; 
    }; 
}); 

我想在$scope.selected價值選擇在下拉菜單中。

回答

0

這是否適合您?

HTML:

<select ui-select2="opt" 
     ng-change="save()" 
     ng-model="chosen" 
     data-placeholder="Choose"> 
     <option value=""></option> 
     <option ng-repeat="item in list" value="{{item}}">{{item.a + ' ' + item.b}}</option> 
</select> 

JS:

app.controller('MainCtrl', function($scope) { 
    $scope.selected = {a: 2, b: 22}; 
    $scope.list = [{a: 1, b: 11}, {a: 2, b: 22}]; 
    $scope.chosen = $scope.selected; 

    $scope.opt = { 
    allowClear: true, 
    initSelection: function (elem, callback) { 
     debugger; // this never triggers... :(
    } 
    }; 

    $scope.save = function() { 
    $scope.pre = $scope.chosen; 
    }; 
}); 
+0

對不起,我不能看出其中的區別。你改變了什麼? – 2014-08-28 11:46:48

+0

找到它'$ scope.chosen = $ scope.selected;'。可悲的是,它並沒有改變任何東西。 – 2014-08-28 12:59:57

+0

當您選擇一個選項時,模型上有什麼? – Wawy 2014-08-28 13:34:54

0

我有同樣的問題,我沒有找到initSelection任何解決方案。

,但我解決它。通過下面這個例子https://github.com/angular-ui/ui-select/blob/master/examples/demo.html

你的情況,你需要做的是這樣的:

<ui-select ng-model="Choose" theme="select2" ng-disabled="disabled"> 
       <ui-select-match>{{$select.selected.a}}</ui-select-match> 
       <ui-select-choices repeat="item in list | filter: {a: $select.search}"> 
        <div ng-bind-html="item.a| highlight: $select.search"></div> 
       </ui-select-choices> 
      </ui-select> 
+0

感謝您的鏈接。我試過了,但不能得到它的工作:( – 2014-08-28 12:06:10

相關問題