【问题标题】:angular ui.select not rendering the items角度 ui.select 不呈现项目
【发布时间】:2016-05-09 23:30:00
【问题描述】:

我正在尝试使用引导主题将 angular ui.select 实现到我的项目中,但除了问题之外我什么都没有。我正在传递一个非常简单的对象,其中包含很少的项目。 这是我试图传递的对象

 [Object { institution_type_id="1",  institution_type_description="Post"}, Object { institution_type_id="2",  institution_type_description="Security"}, Object { institution_type_id="3",  institution_type_description="Mutual Fund"}, Object { institution_type_id="4",  institution_type_description="Bank"}, Object { institution_type_id="5",  institution_type_description="Life insurance"}]

使用如下所示的常规选择可以很好地呈现。

我想改用 angular ui.select,这是我的设置.. 我的主页中包含以下内容

<!-- Angular ui-select -->
    <link rel="stylesheet" href="../bower_components/ui-select/dist/select.min.css" type="text/css" />

<!-- Angular ui-select -->
<script src="../bower_components/ui-select/dist/select.min.js"></script>
<script src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>

我想使用 bootstrap 主题,所以当然也包括 bootstrap css

现在在我的 app.js 中,我已经包含了

ui.select

作为依赖

这是我在控制器中的内容:

institutionService.getAllInstitutionTypes().then(function(data){       
        $scope.institutionTypes =  data;
    });

这是我的 html 的样子。

<div class="form-group required" ng-class="{ 'has-error': addEditInstitutionForm.institution_type_id.$invalid && addEditInstitutionForm.institution_type_id.$dirty}">
                <label for="institution_type_id" class="control-label col-xs-4">Institution Type</label>
                <div class="col-xs-8">
                    <ui-select ng-model="ctrl.country.selected" theme="bootstrap" style="width: 300px;" title="Choose Institution Type">
                        <ui-select-match placeholder="Select or search institutionType...">{{$select.selected.institution_type_description}}</ui-select-match>
                        <ui-select-choices repeat="type in institutionTypes">
                            <span ng-bind-html="type.institution_type_description"></span>
                            <small ng-bind-html="type.institution_type_id"></small>
                        </ui-select-choices>
                    </ui-select>
                </div>

当我加载页面时,看起来 ui.select 渲染得很好。但下拉列表中没有任何内容,如下所示。

我在控制台中看到很多关于 $sce:unsafe 的错误,如下所示。

现在的问题是:

  1. 如何修复这些错误以正确呈现下拉菜单。
  2. 在代码 sn-p (http://angular-ui.github.io/ui-select/demo-basic.html) 中,他们在几个地方引用了 $select。有人可以帮我理解吗?
  3. 如果我设法解决问题并更好地理解它以在我的项目中使用它,我该如何验证 angular ui.select。
  4. 还有比 angular.ui select 更好、更简单的选择吗?

谢谢。

【问题讨论】:

  • 您是否在某处包含了一些外部 URL?这主要是显示错误的原因。
  • 解决你的问题here
  • 没有。我没有在外部加载任何东西。所有 CSS 和 JS 文件都是从项目文件夹中加载的
  • 何塞,我已经尝试使用 $sce.trustedHtml 来解决问题,但由于某种原因它无法正常工作。但是我尝试了 ng-bind-html-unsafe 并清除了一个错误,但我的下拉列表仍然没有呈现。另外,如上所示,我没有将任何 html 传递到我的对象中,为什么我必须将其标记为不安全?
  • 您是否也将 ngSanitize 注入到您的应用程序中?

标签: angularjs ui-select


【解决方案1】:

几个问题。

  1. 需要注入ngSanitize,像这样:

var app = angular.module('plunker', ['ui.select', 'ngSanitize']);

  1. 您在 ng 模型“ctrl.country.selected”中使用了 ControllerAs 模式,但在重复属性“type in authorityTypes”中引用选项数组时没有使用

控制器:

var app = angular.module('plunker', ['ui.select', 'ngSanitize']);

app.controller('MainCtrl', function($scope) {

  var self = this;

  self.institutionTypes = [{
    institution_type_id: "1",
    institution_type_description: "Post"
  }, {
    institution_type_id: "2",
    institution_type_description: "Security"
  }, {
    institution_type_id: "3",
    institution_type_description: "Mutual Fund"
  }, {
    institution_type_id: "4",
    institution_type_description: "Bank"
  }, {
    institution_type_id: "5",
    institution_type_description: "Life insurance"
  }];

  self.country = {
    institution_type_id: "5",
    institution_type_description: "Life insurance"
  };

HTML:

      <ui-select ng-model="ctrl.country" theme="bootstrap">
        <ui-select-match placeholder="Select or search institutionType...">{{$select.selected.institution_type_description}}</ui-select-match>
        <ui-select-choices repeat="type in ctrl.institutionTypes | filter: $select.search ">
          <span ng-bind-html="type.institution_type_description | highlight: $select.search"></span>
        </ui-select-choices>
      </ui-select>

});

这是一个有效的plunker

【讨论】:

  • bBrown,我已将 ng-model 中的问题修复为机构类型.selected,除了现在我可以打印选择的任何内容之外,不会导致任何问题。感谢您指出我。我的 jQwidgets 拆分器面板还有另一个问题,它的高度没有调整到 ui 选择元素,因为它是在事后呈现的。所以现在它渲染得很好。一旦我从它中取出,如何验证 ui 选择是否为空值。我还需要在表单提交时对其进行验证。
  • @AndyJohnson - 如果您有其他问题,您应该单独提交这些问题,而不是让这个问题遇到其他问题。
猜你喜欢
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
相关资源
最近更新 更多