How can I correctly insert(add) an object(nest) into a JSON using Angular?
Check out the live demo here: JSbin Link
I have a factory for Airports with a specific structure:
angApp.factory("Airports", function () {
var Airports = {};
Airports.detail = {
"PDX": {
"code": "PDX",
"name": "Portland International Airport",
"city": "Portland"
},
"STL": {
"code": "STL",
"name": "Lambert-St. Louis International Airport",
"city": "St. Louis"
},
"MCI": {
"code": "MCI",
"name": "Kansas City International Airport",
"city": "Kansas City"
}
};
return Airports;
});
Connected to a Controller: How do I create a proper method to add the input to Airport.details?
.controller("AirportsCtrl", function ($scope, Airports) {
$scope.formURL = "views/_form.html";
$scope.currentAirport = null;
$scope.airports = Airports;
$scope.setAirport = function (code) {
$scope.currentAirport = $scope.airports.detail[code];
};
$scope.addAirport = function() {
$scope.airports.push();
};
});
HTML:
What should go in the ng-model to properly add an object into Airport.details
Add Airport
ID:
<div class="form-group">
<label >Code:</label><br>
<input class="form-control" type="text" placeholder="eg. PDX">
</div>
<div class="form-group">
<label>Name:</label><br>
<input class="form-control" type="text" ng-model="" placeholder="eg. Portland Intl. Airport">
</div>
<div class="form-group">
<label>City</label><br>
<input class="form-control"type="text" ng-model="" placeholder="eg. Portland">
</div>
<input class="btn btn-primary" type="submit">
</form>