【问题标题】:Initialize select2 in AngularJS在 AngularJS 中初始化 select2
【发布时间】:2015-09-26 01:30:12
【问题描述】:

我正在尝试找出在 Angular.JS 中初始化 select2 下拉列表的最佳方法。

目前我所有包含表单的 html 视图都使用相同的控制器,这引发了一个明显的问题“如果想要在带有自定义控制器的页面上使用下拉列表怎么办?”

谁能想到一种更优雅的方式来初始化我的下拉列表?

下面是我的 HTML 视图和表单控制器。

Form.html

    <div class="form-group">
                        <label class="col-md-3 control-label">Model *</label>
                        <div class="col-md-4">
                            <select ng-model="form.Model" class="form-control DDL">
                                <option ng-repeat="item in modelDDL" value="{{item.id}}">{{item.text}}</option>
                            </select>
                            <span class="help-block">

                            </span>
                        </div>
                    </div>

<script>
    $(".DDL").select2();
</script>

FormsController.js:

     //get values for dropdown lists
        $http.get('http://localhost:2982/api/menu?type=model').
  then(function (response) {
      $scope.modelDDL = response.data;

【问题讨论】:

    标签: javascript angularjs jquery-select2


    【解决方案1】:

    我会使用 https://github.com/angular-ui/ui-select 然后你可以使用 ng-model 然后我会使用依赖注入和获取模型数据的服务。你可以在任何控制器中使用 DI。

    【讨论】:

    • ui-select 绝对是最好的选择
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2015-04-28
    • 2017-04-06
    • 2018-09-15
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多