Search code examples
angularjsui-select

ng-style does not work in ui-select-match- AngularJs


I have a ui-select and I need to show a color in a html span tag, I use ng-style to enter the color, in ng-select-choices it works, but in ui-select-match it does not work

<div class="form-group container-fluid">
    <label class="col-md-2 control-label">Categoría:</label>
    <div class="col-md-10">
      <ui-select ng-model="activity.category"
                 theme="bootstrap"
                 title="Selecciona una categoría">
        <ui-select-match placeholder="Selecciona una categoría">
          {{ $select.selected.name }} 
          <span style="width: 10px;
                       height: 10px;
                       border-radius: 50%;
                       display: inline-block"
                ng-style="{'background-color': '{{$select.selected.color}}'}">
          </span>
        </ui-select-match>
        <ui-select-choices repeat="category in categories | filter: $select.search">
          {{ category.name }} 
          <span style="width: 10px; height: 10px;
                       border-radius: 50%; display: inline-block"
                ng-style="{'background-color': '{{category.color}}'}">
          </span>
        </ui-select-choices>
      </ui-select>
    </div>
</div>

Why does not it work? Is there any way I can make it work?


Solution

  • Remove the double curly bracket {{ }} interpolation from the expression:

    <div class="form-group container-fluid">
        <label class="col-md-2 control-label">Categoría:</label>
        <div class="col-md-10">
          <ui-select ng-model="activity.category"
                     theme="bootstrap"
                     title="Selecciona una categoría">
            <ui-select-match placeholder="Selecciona una categoría">
              {{ $select.selected.name }} 
              <span style="width: 10px;
                           height: 10px;
                           border-radius: 50%;
                           display: inline-block"
                    ̶n̶g̶-̶s̶t̶y̶l̶e̶=̶"̶{̶'̶b̶a̶c̶k̶g̶r̶o̶u̶n̶d̶-̶c̶o̶l̶o̶r̶'̶:̶ ̶'̶{̶{̶$̶s̶e̶l̶e̶c̶t̶.̶s̶e̶l̶e̶c̶t̶e̶d̶.̶c̶o̶l̶o̶r̶}̶}̶'̶}̶"̶
                    ng-style="{'background-color': $select.selected.color}" >
              </span>
            </ui-select-match>
            <ui-select-choices repeat="category in categories | filter: $select.search">
              {{ category.name }} 
              <span style="width: 10px; height: 10px;
                           border-radius: 50%; display: inline-block"
                    ̶n̶g̶-̶s̶t̶y̶l̶e̶=̶"̶{̶'̶b̶a̶c̶k̶g̶r̶o̶u̶n̶d̶-̶c̶o̶l̶o̶r̶'̶:̶ ̶'̶{̶{̶c̶a̶t̶e̶g̶o̶r̶y̶.̶c̶o̶l̶o̶r̶}̶}̶'̶}̶"̶
                    ng-style="{'background-color': category.color}" >
              </span>
            </ui-select-choices>
          </ui-select>
        </div>
    </div>
    

    There is no guarantee that it works for every directive, because interpolation itself is a directive. If another directive accesses attribute data before interpolation has run, it will get the raw interpolation markup and not data.

    For more information, see