How to get values from ng-model array into input

1,765 views
Skip to first unread message

Maksim Go

unread,
Mar 18, 2014, 1:48:35 PM3/18/14
to ang...@googlegroups.com
Hello,

I need help with getting values from array into comma separated list.

I have select tag: <select ng-model="selected_item" name="select" id="gallery_list" ng-options="v as v.Title for (k, v) in Items"></select>

now if I need to get selected items tags with {{selected_item.Tags}} it gives me an array: {"0":" Test Tag 1","1":"Test Tag 2"}
But since i need to display this list as input value, I don't know how to iterate the values in given array:
<input type="text" name="Tags" value="{{selected_item.Tags}}" placeholder="Tag, Tag 2"/>

Snippet what I have is like this:
<select ng-model="selected_item" name="select" id="gallery_list" ng-options="v as v.Title for (k, v) in Items"></select>
<input type="text" name="Tags" value="{{selected_item.Tags}}" placeholder="Tag, Tag 2"/>

Luke Kende

unread,
Mar 19, 2014, 1:29:25 AM3/19/14
to ang...@googlegroups.com
Ok, so you've got selected_item bound to ng-model and it is an object with a property of Tags that points to an array.  Instead of trying interpolation on the value attribute of the input, make the input use ng-model and update the value when the selected item changes with ng-change.  This is the more "angular way" of doing it.

<select ng-model="selected_item" name="select" id="gallery_list" ng-options="v as v.Title for (k, v) in Items" ng-change="updateTags()"></select>
<input type="text" name="Tags" ng-model="selectedTags" placeholder="Tag, Tag 2"/>

//in the controller
$scope.selectedTags = null;

$scope. updateTags = function(){
  $scope.selectedTags = $scope.selected_item.Tags.join(); //join() creates a comma separated string of values from the array

Maksim Go

unread,
Mar 19, 2014, 6:38:07 AM3/19/14
to ang...@googlegroups.com
Thank you, that works!

Ziobudda

unread,
Mar 19, 2014, 9:10:50 AM3/19/14
to ang...@googlegroups.com
Il giorno 19/mar/2014, alle ore 06:29, Luke Kende <luke....@gmail.com> ha scritto:

Ok, so you've got selected_item bound to ng-model and it is an object with a property of Tags that points to an array.  Instead of trying interpolation on the value attribute of the input, make the input use ng-model and update the value when the selected item changes with ng-change.  This is the more "angular way" of doing it.

<select ng-model="selected_item" name="select" id="gallery_list" ng-options="v as v.Title for (k, v) in Items" ng-change="updateTags()"></select>
<input type="text" name="Tags" ng-model="selectedTags" placeholder="Tag, Tag 2"/>

//in the controller
$scope.selectedTags = null;

$scope. updateTags = function(){
  $scope.selectedTags = $scope.selected_item.Tags.join(); //join() creates a comma separated string of values from the array
}

Hi, can you explain the last line ? 
I don’t understand  $scope.selected_item.Tags.

Ok for $scope, but “selected_item” is the ng-model of <select> while “Tags” is “name" of “input text”.

Thanks.

M.

--
Davide Morelli




Luke Kende

unread,
Mar 20, 2014, 2:16:47 AM3/20/14
to ang...@googlegroups.com
The fact that the input has name="Tags" is irrelevant.  

selected_item.Tags existed in his object as an array, like,

$scope.selected_item = {
  Tags: [ 'tag1', 'tag2', ... and so on ]
}

hence selected_item.Tags points to the array
Reply all
Reply to author
Forward
0 new messages