Ad
How To Display Table From Json URL By AngularJS?
i'm new to Angular JS, and i'm learning how to create a table from the URL. I found this code online to show how to display the information in the URL into table but it wont work, can you guys help me check this out. Thank you.
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-controller="planetController">
<table>
<tr>
<th>Planet</th>
<th>Distance</th>
</tr>
<tr ng-repeat="x in names">
<td>{{ x.name}}</td>
<td>{{ x.distance}}</td>
</tr>
</table>
</div>
<script>
function planetController($scope, $http) {
$http.get("http://www.bogotobogo.com/AngularJS/files/Tables/planet.json")
.success(function(response) {$scope.names = response;});
}
</script>
</body>
</html>
Ad
Answer
your code seems to fine.
Check your URL response by printing the response in Browser console. I think, you are getting CORS error for the URL(http://www.bogotobogo.com/AngularJS/files/Tables/planet.json).
Just go through below code for creating table using local data instead of API/URL.
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.name }}</td>
<td>{{ x.distance }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.names= [
{"name":"Neptune", "distance":30.087},
{"name":"Uranus", "distance":19.208},
{"name":"Saturn", "distance":9.523},
{"name":"Jupiter", "distance":5.203},
{"name":"Mars", "distance":1.524},
{"name":"Earth", "distance":1.0},
{"name":"Venus", "distance":0.723},
{"name":"Mercury", "distance":0.387}
]
});
</script>
</body>
</html>
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad