EDIT - I have updated my quetion with more simplified version of what I want to do, but I will keep the original version as well.. (to keep the answers align..)
I'm fairly a beginner for AngularJS
and currently having problems when trying to bind data to my select
tag
What I want to do
- user clicks the
edit
link - existing object loads to the page (via back end
api
) - object value should be
selected
in the drpodown
Following is my code
#in the HTML page
<select ng-model="FormData.name"
ng-options="n.name for n in names"
class="form-control input-lg no-border-radius" required>
</select>
#in my service I have defined the `names` array
names: [{name: 'Superman', val: '1'}, {name: 'Batman', val: '2'}]
#this is what I do in initial form loading
$scope.name = <service name>.names;
$scope.FormData.name = $scope.names[0];
So I save the value 1
or 2
in the DB and getting it back. If the GET
request is successful, I do
$scope.FormData = data; #data is the object form the server
So the problem is, when I do this, all the values are setting in the form correctly except
select tags
How can I set selected values for my select tags depending on the server request.
New simplified version of my question
var names= [{name: 'Superman', val: '1'}, {name: 'Batman', val: '2'}];
$scope.names =names;
$scope.FormData = {};
$scope.FormData.name = $scope.names[1];
In the following array, instead of selecting the item by index, how can I select the item by name
or the value
, like
$scope.FormData.name = $scope.names['2']; #will select the {name: 'Batman', val: '2'}