2

I have an HTML file with a form and json file with the following structure:

[
        {
           "airline" : "British Airways",
           "hub"     : "LHR",
           "url"       : "http://britishairways.com",
           "callsign":"SPEEDBIRD"     
        },
        {
           "airline" : "Air France",
           "hub"     : "CDG",
           "url"       : "http://airfrance.com",       
           "callsign":"AIRFRANS"        
         },
        {
           "airline" : "Virgin Atlantic",
           "hub"     : "LGW",
           "url"       : "http://virginatlantic.com",
          "callsign":"VIRGIN"       
        },
        {
           "airline"   : "RyanAir",
           "hub"       : "DUB",
           "url"         : "http://ryanair.com",  
           "callsign":"RYANAIR"     
        },
        {
           "airline" : "ANA",
           "hub"     : "HND",
           "url"       : "http://ana.com.jp",
           "callsign":"ALL NIPPON"       
        },
        {
           "airline" : "Flydubai",
           "hub"     : "DXB",
           "url"       : "http://flydubai.com",
           "callsign":"SKY DUBAI"       
        }
]

AngularJS parameters: ng-app="ajsprogram", ng-controller="outercont".

How do I add a new data to it from my form through a function on submit?

The data structure is pretty much the same:

  "airline" : "Emirates",
   "hub"     : "DXB",
   "url"       : "http://emirates.com", 
   "callsign":"EMIRATES" 
4
  • Is there a database or web service or API involved in this project or just static files. Typically I would say that you would use ngResource to make a call to an API to post the data. Please provide more info Commented May 29, 2018 at 17:46
  • 1
    The javascript code running in the browser cannot directly write to a file on the web server. You should consider creating an API to run code on the server side or using a database to store the data. Commented May 29, 2018 at 17:47
  • 1
    Use .push on the Array. For more information, see MDN JavaScript Reference - Array push(). Commented May 29, 2018 at 18:30
  • There are only static files here (no API involved). I've found similar question here: stackoverflow.com/questions/37839731/… They use .push method, however I am not sure how to apply to a .json file itself. (I am new to AngularJS) Commented May 29, 2018 at 18:45

2 Answers 2

2

You can create an array and simply push your form data in this array like following:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="ajsprogram" ng-controller="outercont">
Airline: <input type="text" ng-model="formData.airline"><br>
Hub: <input type="text" ng-model="formData.hub"><br>
Url: <input type="text" ng-model="formData.url"><br>
Call sign: <input type="text" ng-model="formData.callsign"><br>
<button ng-click="insertData()">Insert</button>
<br>
The Data Is : {{array}}
</div>

<script>
var app = angular.module('ajsprogram', []);
app.controller('outercont', function($scope) {
	
    $scope.array = [
        {
           "airline" : "British Airways",
           "hub"     : "LHR",
           "url"       : "http://britishairways.com",
           "callsign":"SPEEDBIRD"     
        },
        {
           "airline" : "Air France",
           "hub"     : "CDG",
           "url"       : "http://airfrance.com",       
           "callsign":"AIRFRANS"        
         },
        {
           "airline" : "Virgin Atlantic",
           "hub"     : "LGW",
           "url"       : "http://virginatlantic.com",
          "callsign":"VIRGIN"       
        },
        {
           "airline"   : "RyanAir",
           "hub"       : "DUB",
           "url"         : "http://ryanair.com",  
           "callsign":"RYANAIR"     
        },
        {
           "airline" : "ANA",
           "hub"     : "HND",
           "url"       : "http://ana.com.jp",
           "callsign":"ALL NIPPON"       
        },
        {
           "airline" : "Flydubai",
           "hub"     : "DXB",
           "url"       : "http://flydubai.com",
           "callsign":"SKY DUBAI"       
        }
	];
    
    $scope.insertData = function(){
    	$scope.array.push($scope.formData);
    }
});
</script>

</body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks, Sudhir. This is exactly what I wanted. I just need this to be in separate files. I've got script.js for all scripts and it contains the code for displaying what is currently in airlines.json file:

var prg = angular.module('ajsprogram', []);

prg.controller("outercont", function($scope, $http) {
        $http.get('airlines.json').success(function(data){
            $scope.info = data;
            $scope.airlineorder = 'airline';
        });
});

And I need the insertData function written there. Here is my .html file with form:

<!DOCTYPE HTML>
<html ng-app="ajsprogram">
<head>
<title>Air Carriers</title>
   <script src="../lib/angular.js"></script>
   <script src="script.js"></script>
</head>
<style>
:root{text-align:center;}
</style>
<body>
<h2 align="center">Air Carriers</h2>
<div ng-controller="outercont">
<p align="center">
   <label>Search:</label><input type="search" ng-model="query"><br><br>
   <select ng-model="airlineorder">
         <option value="airline" selected>Airline Name</option>
         <option value="hub">Domestic Airport</option>
         <option value="callsign">Callsign</option>
   </select><br>
   <input type="radio" ng-model="direction" name="direction" checked>ASC<br>
  <input type="radio" ng-model="direction" name="direction" value="reverse">DESC
</p>
   <table align="center">
      <th>Airline</th><th>Hub</th><th>URL</th><th>Callsign</th>
      <tr ng-repeat="item in info | filter: query | orderBy: airlineorder: direction">
<td>{{item.airline}}</td><td>{{item.hub}}</td><td><a href="{{item.url}}">{{item.url}}</a></td><td>{{item.callsign}}</td>
</tr>
   </table>
</div>
<br>
New Airline Information:
<form onsubmit="newairline(an.value,hb.value,ur.value,cs.value)">
<table align="center">
<th>Airline:</th><th>Hub:</th><th>URL:</th><th>Callsign:</th>
<tr>
<td><input type="text" id="an" name="alname" value=""></td>
<td><input type="text" id="hb" size="3" maxlength="3" name="hub" value="" onkeyup="this.value=this.value.toUpperCase();"></td>
<td><input type="text" id="ur" name="url" value=""></td>
<td><input type="text" id="cs" name="cs" value="" onkeyup="this.value=this.value.toUpperCase();"></td>
</tr>
    <tr>
</table>
<input type="submit" value="new airline" style="cursor:pointer;">
</form>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.