【问题标题】:How to set selected when using ngRepeat on objects在对象上使用 ngRepeat 时如何设置选中
【发布时间】:2014-08-28 10:08:09
【问题描述】:

我正在尝试设置 select2 下拉菜单的初始值。问题是该值是一个对象。 我试着弄乱initSelection,但我什至无法让它调用它。我将debugger 放入其中,但没有任何反应。 ngSelected 也不起作用 :(

我发了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 中的值。

【问题讨论】:

    标签: angularjs jquery-select2 ui-select2


    【解决方案1】:

    这对你有用吗?

    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;
      };
    });
    

    【讨论】:

    • 抱歉,我看不出区别。你改变了什么?
    • 找到它$scope.chosen = $scope.selected;。遗憾的是它并没有改变任何东西。
    • 当您选择一个选项时,模型上有什么?
    【解决方案2】:

    我遇到了同样的问题,我没有找到 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>
    

    【讨论】:

    • 感谢您的链接。我试过了,但不能让它工作:(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多