1

I currently have checkboxes that the user can choose between which then are sent to my model,

<div ng-repeat="option in roleOptions">
    <input type="checkbox" value="{{option.Id}}" ng-model="user.Roles[option.Id]">{{option.Name}}
</div>

The problem is that this produces a result like this,

{
  0: 'a',
  1: 'b',
  2: 'c'
}

Whereas I want an output like this,

['a','b','c']

I realise that I could just convert them after, but I'd really like to change my html in some way so that it maps straight to an array, because I will also be sending this data in an array format to the html as well so I don't want to have to keep converting all the time.

Any ideas?

1 Answer 1

1

ng-model only works on an object. You are trying to push a value onto an array. You are probably best of using ng-click and creating your own handler to push / shift from the array.

<div ng-repeat="option in roleOptions">
    <input type="checkbox" value="{{option.Id}}" ng-click="addToArray(user.Roles, option.Id)">{{option.Name}}
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

OK thanks. I guess I'll have to do it the awkward way and convert it back and forth.
Shouldn't be that awkward. Just create a function which add/removes values from the array you are using. No more fuzz needed :)
@user3407039 If this answer was satisfactory, please accept it.

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.