1

I am trying to implement the angular ui.select into my project using bootstrap theme but I have nothing but problems with it. I am passing a very simple object of few items in it. Here is object that I am trying to pass

 [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"}]

It renders fine using a regular select as seen below.

Regular Select

I want to use the angular ui.select instead and here is my setup.. I have included the following in my main page

<!-- 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>

I want to use the bootstrap theme so of course bootstrap css is included as well

Now in my app.js, I have included the

ui.select

as dependency

Here is what I have in controller:

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

and here is my html looks like.

<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>

when I load the page, it looks like the ui.select has rendered fine.. but there is nothing inside the dropdown as seen below. angular ui.select dropdown

what I see in console is lot of errors about $sce:unsafe as seen below. $sce:unsafe error

Questions now are:

  1. How can I fix these errors to render the dropdown correctly.
  2. In the code snippet (http://angular-ui.github.io/ui-select/demo-basic.html), they are referencing $select on several places. Can someone please help me make understand it?
  3. How can I validate the angular ui.select if I manage to fix the issue and understand it better enough to use it on my project.
  4. Is there any better and simpler choice available than angular.ui select?

Thanks.

derOtterDieb
  • 545
  • 4
  • 12
  • 35
Andy Johnson
  • 639
  • 8
  • 27
  • Are you including some external URL somewhere? That's mostly the cause of the error shown. – Matheno May 09 '16 at 14:24
  • The solution to your problem [here](http://stackoverflow.com/questions/18340872/how-do-you-use-sce-trustashtmlstring-to-replicate-ng-bind-html-unsafe-in-angu) – Jose Rocha May 09 '16 at 14:30
  • No. I am not loading anything externally. All CSS and JS files are loaded from within project folders – Andy Johnson May 09 '16 at 14:33
  • Jose, I have tried it use the $sce.trustedHtml to fix the issue but it was not working for some reason. But I tried the ng-bind-html-unsafe and that clears an error but my dropdown still doesn't render. Also I am not passing any html into my object as seen above, why I have to mark it as unsafe? – Andy Johnson May 09 '16 at 14:37
  • Did you inject ngSanitize to your app as well? – jbrown May 09 '16 at 14:37
  • @AndyJohnson I could be wrong but since you have `ng-bind-html ` you are saying that the value inside that will be html even it is just text like "Hello world!". Instead of using `` try something like `{{type.institution_type_description}}`. – Jose Rocha May 09 '16 at 14:40
  • jBrown, I have injected the ngSanitize and now the $scope:unsafe error is gone but looks like it just renders my first choice only. – Andy Johnson May 09 '16 at 14:43
  • One thing I noticed is that you are using the ControllerAs pattern in your ng-model, "ctrl.country.selected", but not when referencing your option array in the repeat attribute, "type in institutionTypes" – jbrown May 09 '16 at 15:09

1 Answers1

0

A couple of issues.

  1. Need to inject ngSanitize, like this:

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

  1. You are using the ControllerAs pattern in your ng-model, "ctrl.country.selected", but not when referencing your option array in the repeat attribute, "type in institutionTypes"

Controller:

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>

});

Here's a working plunker

jbrown
  • 3,025
  • 1
  • 15
  • 22
  • bBrown, I have fixed the issue in ng-model to be institutionTypes.selected and that is not causing anything other than now I can print whatever is selected. Thanks for pointing me out.. There was another problem with my jQwidgets splitter panel where height of it was not adjusting to the ui select element since it is rendered after the fact. So now it renders just fine. How can I validate the ui select for an empty value as soon as I tab out of it. Also I need to validate it on form submit. – Andy Johnson May 09 '16 at 15:38
  • @AndyJohnson - if you have additional questions you should submit those individually rather than have this question run on into other questions. – jbrown May 09 '16 at 15:57