I am trying to populate JSON data to HTML Dropdown and I did it successfuly.
Now what I want is:
To get ID of selected listitem when I click on it (id which is coming in JSON not the DOM id) So I did it like this:
$http({
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Accept': 'application/json'
},
url: 'someURL',
method: 'GET',
})
.then(function(response, status, headers, config) {
var myData1 = JSON.parse(response.data);
// Preparing data to send to DOM
var myNames = [];
$.each(myData1, function(index, value){
// I can use some real data instead of 'Some static string' but just because of ease. I am passing it hard coded.
myNames.push({"name": "Some static string",
"id": 1});
})
/* This is with real data
$.each(myData1, function(index, value){
myNames.push({"name": value.stationNameEn,
"id": value.stationNameid});
})
*/
$scope.names = JSON.stringify(myNames);
console.log(myNames);
})
.catch(function(error, status, headers, config){
console.log("Some problem occured: " + JSON.stringify(error));
})
// Sending data to DOM
$scope.formData = {};
$scope.dropboxitemselected = function (item) {
$scope.formData.selectedItem = item;
console.log($scope.formData.selectedName);
}
On my DOM I have:
<select class="ion-input-select padding"
ng-model="formData.selectedName"
ng-options="x for x in names"
ng-change="dropboxitemselected();"
>
</select>
selectedName: {{formData.selectedName}}
Problem:
I am getting all results as Objects, when I JSON.stringify() it, it splits into individual letter, like:
r
e
s
u
l
t
s
What I want is:
Getting id which is coming from JSON, when I click on list item.
JSON.stringifythemyNames.. just$scope.names = myNamesmaybe?objectandobjectandobject. So I don't know what to do.dropdownitem of html I getidof it (id not of index, but id of database).