CrazzyMarc CrazzyMarc - 2 months ago 10
AngularJS Question

AngularJS, how to use JSON from child in parent?

When an item is selected in the select box, I want to use the other field in the parent div. In this case the country.

My code:


index.php

<div class="field" id="{{$index+1}}" ng-repeat="t in getTimes(fieldSelect) track by $index")>
<select name="drop_down" class="marketSelect">
<option ng-repeat="item in items">{{item.name}}</option>
</select>
{{item.country}}
</div>


angular.js

$http({
method: 'GET',
url: 'assets/json/currency.json'
}).then(function successCallback(response) {
$scope.items = response.data.currency;
}, function errorCallback(response) {
console.log("Oh shit, JSON is not working");
});

Answer

Use the ng-model directive in combination with the ng-options:

<select ng-options="item as item.name for item in items" 
        ng-model="selectedItem">
</select>

Then in your div:

{{ selectedItem.country }}

See this jsfiddle

Comments