ui-select icon indicating copy to clipboard operation
ui-select copied to clipboard

Wrong select-choice highlighted on load

Open TheArchitect78 opened this issue 10 years ago • 6 comments

When my app loads for first time I am having an issue with the wrong select choice being highlighted in the drop down. The value is correct in my inspector and the select-match shows the correct choice. but the drop down for some reason is highlighting the wrong one.

Once the app has loaded and you change choices the correct one is highlighted. This only happens on first load.

Anyone ever experience this?

screen shot 2016-02-19 at 1 34 14 pm

screen shot 2016-02-19 at 1 34 20 pm

TheArchitect78 avatar Feb 19 '16 20:02 TheArchitect78

I don't think there is enough information to help here, verify that your ngModel value is set correctly. Can you create a plunkr or similar to reproduce? Maybe that will help.

user378230 avatar Feb 19 '16 22:02 user378230

The ngModel is set correctly because the select-match is correct. Not very good with plunkr :( Below is the relevant code.

<ui-select on-select="app.changed($select)" ng-model="selectedClient">
    <ui-select-match>{{$select.selected.name}}</ui-select-match>
    <ui-select-choices repeat="client in app.clients | filter: {name: $select.search}">
        <span ng-bind-html="client.name | highlight: $select.search"></span>
    </ui-select-choices>
</ui-select>

    var app = this;
    app.user = user.data;
    app.clients = clients.data;

    app.setClient = function(){
      if (!store.get('selectedClient')) {
        store.set('selectedClient', clients.data[0]);
      };
      $rootScope.selectedClient = store.get('selectedClient')
    };

    app.setClient();

    app.logout = function () {
      AuthService.logout();
    };

    app.changed = function ($select) {
      $rootScope.selectedClient = $select.selected;
      store.set('selectedClient', $select.selected);
      $rootScope.$broadcast('client-changed');
    };

TheArchitect78 avatar Feb 19 '16 22:02 TheArchitect78

You need to fix your ngModel reference first. See https://github.com/angular-ui/ui-select/issues/1353#issuecomment-175729671 and FAQ

user378230 avatar Feb 19 '16 22:02 user378230

But if the ngModel was messed up wouldn't {{$select.selected.name}} return the wrong value?

TheArchitect78 avatar Feb 19 '16 22:02 TheArchitect78

Never mind. I get it now. selectedClient is an object not an item in array. so it doesn't know which item in the array to select

TheArchitect78 avatar Feb 19 '16 22:02 TheArchitect78

Just to update incase anyone else gets stumped by this. @user378230 Thanks for the help!

                                <ui-select on-select="app.changed($select)" ng-model="app.selected.item">
                                    <ui-select-match>{{$select.selected.name}}</ui-select-match>
                                    <ui-select-choices repeat="client in app.clients | filter: {name: $select.search}">
                                        <span ng-bind-html="client.name | highlight: $select.search"></span>
                                    </ui-select-choices>
                                </ui-select>

    var app = this;
    app.user = user.data;
    app.clients = clients.data;

    app.setClient = function(){
      if (!store.get('selectedClient')) {
        store.set('selectedClient', clients.data[0]);
      };
      $rootScope.selectedClient = store.get('selectedClient');
      app.item = $filter('filter')(app.clients, function (d) {return d.id === $rootScope.selectedClient.id;})[0];
      app.selected = { item: app.item };
    };

    app.setClient();

    app.logout = function () {
      AuthService.logout();
    };

    app.changed = function ($select) {
      $rootScope.selectedClient = $select.selected;
      store.set('selectedClient', $select.selected);
      $rootScope.$broadcast('client-changed');
    };

TheArchitect78 avatar Feb 19 '16 22:02 TheArchitect78