How do I properly add(push) a object(nest) to a json using angular.
See live working demo on: JSbin Link
I have a factory of Airports with a particular 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;
});
Linked with a Controller: How do i write a proper method to push the input to Airport.detail?
.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 do i put into ng-model to push an objected into Airport.details properly
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>