1

I'm trying to use angularjs with multiple html select fields and I want to store all the selected elements in an array.

May code would look like this:

<body ng-app="" ng-controller="myController">
{{selectedElements}}
<div ng-repeat="elem in selectedElements">
    <select 
        ng-model="elem.someThing"   <-- I do not want the someThing here
        ng-options="obj.name for obj in objects">
     </select>

    {{elem}}
</div>
<script>
    var myController = function ($scope) {
        $scope.selectedElements = [{},{}]

        $scope.objects = [
            {"id": "1234","name": "a"}, 
            {"id": "12345","name": "b"},
            {"id": "123456","name": "b"},
            {"id": "123458","name": "c"}
        ];
    };
</script>

You can also find a fiddle here: http://jsfiddle.net/dg76hetu/12/

The issue is, that I do not really want to use:

ng-model="elem.someThing"

because my selectedElements Array then includes "someThing" as a key. (see in fiddle) Intuitively I would want to do something like:

ng-model="elem"

However, this does not work and I am also aware that I should not bind to the model without the dot notation.

So my question is, how can I use angular to store all the selected objects from the multiple select fields inside the "selectedElements" Array? I feel like I am missing something basic, however I just can't get it to work.

Any help is highly appreciated.

2
  • 1
    jsfiddle.net/dg76hetu/13 Commented Nov 16, 2015 at 16:45
  • You need a model per each element in selectedElements, otherwise you will be binding both values in the view to the same controller variable. Commented Nov 16, 2015 at 16:51

1 Answer 1

2

You can link ng-model to actual value using $index and passing array[$index] in to ng-model directly.

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for you quick answer. This actually works great. However, I would also like to have the html select element be initialised if my selectedElements Array would as an example look like so: $scope.selectedElements = [{"id": "1234","name": "a"},{}]. Any idea on how that could be realised with your solution?
say my array called "$scope.selectedElements" is already initialised with some object(s) instead of those empty objects[{},{}], I would like this to be reflected in the html select field. In other words, the html select field should then already be preselected if the array called "$scope.selectedElements" already contains an element at the beginning. Like this: jsfiddle.net/z1aujv1x
You need to link actual Object but not copy of them. To do that create linking function that will run though selectedElements, find appropriate actual Object in objects and link it to origin selectedElement[i] = objects[j]
jsfiddle.net/dg76hetu/15 here is what u want to achieve (with that function that link Objects)
exactly. This is perfect. Thanks a lot :)

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.