UI选择范围模型值中的选择框数组无法正确显示

时间:2019-02-07 05:17:40

标签: javascript jquery angularjs angularjs-directive

对不起,我的英语。 我正在使用UI-Select选择框来查看数据,但是我正在使用ng-model值打印未定义的数据。我正在使用基本数组。

1 个答案:

答案 0 :(得分:1)

可以通过在控制器中初始化一个对象并更新该对象的属性而不是直接更新范围变量来解决此问题。

请访问此link了解更多信息。

下面是该解决方案的有效示例。

'use strict';

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

app.controller('DemoCtrl', function($scope, $http, $timeout, $interval) {
  $scope.karthi = [1, 2, 3, 4, 5, 6, 10];
  $scope.ctrl = {};
});
<!DOCTYPE html>
<html lang="en" ng-app="demo">

<head>
  <meta charset="utf-8">
  <title>AngularJS ui-select</title>

  <!--
      IE8 support, see AngularJS Internet Explorer Compatibility https://docs.angularjs.org/guide/ie
      For Firefox 3.6, you will also need to include jQuery and ECMAScript 5 shim
    -->
  <!--[if lt IE 9]>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/2.2.0/es5-shim.js"></script>
      <script>
        document.createElement('ui-select');
        document.createElement('ui-select-match');
        document.createElement('ui-select-choices');
      </script>
    <![endif]-->

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-sanitize.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.js"></script>
  <!-- ui-select files -->
  <script src="./select.js"></script>
  <link rel="stylesheet" href="./select.css">

  <script src="./demo.js"></script>

  <!-- themes -->
  <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.default.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-select/0.20.0/select.min.css" />
  <style>
    body {
      padding: 15px;
    }
    
    .select2>.select2-choice.ui-select-match {
      /* Because of the inclusion of Bootstrap */
      height: 29px;
    }
    
    .selectize-control>.selectize-dropdown {
      top: 36px;
    }
    /* Some additional styling to demonstrate that append-to-body helps achieve the proper z-index layering. */
    
    .select-box {
      background: #fff;
      position: relative;
      z-index: 1;
    }
    
    .alert-info.positioned {
      margin-top: 1em;
      position: relative;
      z-index: 10000;
      /* The select2 dropdown has a z-index of 9999 */
    }
  </style>
</head>

<body>
  <div ng-controller="DemoCtrl">


    <h3>Selectize theme</h3>
    <p>Selected: {{ctrl.asdf}}</p>
    <ui-select ng-model="ctrl.asdf" theme="selectize" ng-disabled="disabled" style="width: 300px;" title="Choose a country">
      <ui-select-match placeholder="Select or search a country in the list...">{{$select.selected}}</ui-select-match>
      <ui-select-choices repeat="country in karthi | filter: $select.search">
        <span ng-bind-html="country | highlight: $select.search"></span>
        <small ng-bind-html="country.code | highlight: $select.search"></small>
      </ui-select-choices>
    </ui-select>
  </div>
</body>

</html>