2

I've bound a model to an ui-select like so

<ui-select multiple ng-model="selectedPeople" theme="bootstrap" style="width:300px;">

However, neither the $scope.selectedPeople gets updated upon selection, nor the ui-select choices are updated when the array is manually changed.

See plunker here

What i'm trying to do here is to programmatically add an item in the ui-select listing. How to?

6
  • You are trying to push in a selection from an external function, yes? Commented May 2, 2015 at 11:18
  • Yes that's what i'm tring to do Commented May 2, 2015 at 11:35
  • ok check my answer...should be what you are looking for. Commented May 2, 2015 at 11:36
  • The "alter model" seems to work fine, while the "append" button doesn't Commented May 2, 2015 at 11:39
  • Are you wanting to add to the choices or add to the selection? or both? Commented May 2, 2015 at 11:40

4 Answers 4

7

Here's a working Plunker.

Make selectedPeople a property of a scope object:

JS

$scope.test = {};

Markup

<ui-select multiple ng-model="test.selectedPeople" theme="bootstrap" style="width:300px;">

...

<pre>{{ test.selectedPeople }}</pre>

“Whenever you have ng-model there’s gotta be a dot in there somewhere. If you don’t have a dot, you’re doing it wrong.” - http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

Edit: to alter the model make this change:

$scope.test = function() {
  $scope.people = [
    { name: 'A',    email: '[email protected]',    age: 10 },
    { name: 'B',    email: '[email protected]',    age: 12 },
  ];
}
Sign up to request clarification or add additional context in comments.

5 Comments

Your plunker doesn't seem to work, clicking the buttons has no effect for me
uhm, still i can't see the values appear in the select, they're just added to the model object
@camden_kid Punker view does not show the pushed items, although the model is correctly updated
@CarlosBarcelona Could you explain what you mean? Clicking "Alter model" does change the options in the input.
@camden_kid, after clicking "Alter model" nothing happens, neither in the select box nor the "selectedPeople contents". Clicking on "append some item" does push values into contents, but not in the select box. I've tested on Chrome (Mac and Windows)
1

it's bug! use like this ng-model="someObject.selectedPeople" every things solved!

$scope.someObject = {
   selectedPeople: []
};

Comments

0

Working Demo

The way I have accomplished this before is to add a $watch function inside the ui-select directive's link function, at the end:

ui-select directive source:

 scope.$watch(function() {
      return scope.$parent.valToPush;
      }, function(newVal) {
        $select.selected.push(newVal);
    })

Controller:

$scope.test = function() {
    $scope.valToPush = 
      { name: 'A',    email: '[email protected]',    age: 10 }
    ;
  }

Now in your controller $scope, assign whatever you want to push into the ui-select to $scope.valToPush.

The object that ui-select holds selected items in is called $select.selected, so that's ultimately where anything you want displayed has to be put.

Comments

0

In order to make it work, you have to use the controllerAs syntax, or encapsulate your model inside an object in your controller. See the Snippet:

angular.module('myApp',['ui.select']).controller('MyController', function ($scope) {
  console.log("mycontroller");
  $scope.myUiSelect={model:{}}; // encapsulate your model inside an object.
  $scope.availableData=["a","b","c"]; //some random options
  $scope.onDatasetChanged = function(){
//    console.log("selected data",$scope.myUiSelect);
  }
});
<link href="https://rawgit.com/angular-ui/ui-select/master/dist/select.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://rawgit.com/angular-ui/ui-select/master/dist/select.js"></script>
<body ng-app="myApp" ng-controller="MyController">
  <ui-select multiple ng-model="myUiSelect.model" close-on-select="false" title="Choose a dataset" ng-change="onDatasetChanged()">
    <ui-select-match placeholder="Select something">{{$item.label}}</ui-select-match>
      <ui-select-choices repeat="data in availableData | filter:$select.search">
                        {{data}}
      </ui-select-choices>
  </ui-select>
  <p>{{myUiSelect}}</p> <!-- Print your model stored inside myUiSelect object -->
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.