Search code examples
javascriptangularjsangular-nglist

How can I get ng-list behavior, but with a slightly different model?


For ng-list, you'd normally have a simple array for your model:

[1,2,3,4,5]

Instead, I have an array of objects like such:

[{ id: 1, value: 2 }, { id: 2, value: 3 } ... ]

Is there any way for me to be able to output each value in a text input similar to this?:

2, 3

Seemingly ng-list is what I want, but obviously my model is wrong. Will I just have to translate my model or is there a way to do this with what I have?

Thanks!


Solution

  • Expanding on @Benjamin Gruenbaum comment which i think is the simplest solution:

     <input type="text" ng-model="test">
    

    and in your controller

     $scope.test = [{ id: 1, value: 2 }, { id: 2, value: 3 } ].map(function(x){ return x.value; });