1. Using $ http We can use the built-in $ http service to communicate directly with the outside world. The $ http service simply encapsulates the browser's native XMLHttpRequest object. The $ http service is a function that can only accept one parameter. This parameter is an object that contains the configuration content used to generate the HTTP request. This function returns a promise object with two methods, success and error.
The most basic usage scenarios of these two methods are as follows:
$ http ({
method: 'GET',
url: '/api/users.json'
}). success (function (data, status, headers, config) {
// Called when the corresponding is ready
}). error (function (data, status, headers, config) {
// Called when the response returns in an error state
});
Second, AngularJS gets JSON data instance code
1.data.json:
<script src = "js / angular-1.3.0.js"> </ script>
<script>
var myApp = angular.module ("myApp", []);
myApp.controller ("myController", function ($ scope, $ http) {
var dataUrl = "json / data.json";
//retrieve data
$ http.get (dataUrl) .success (function (data) {
$ scope.students = data;
}). error (function () {
alert ("Error");
});
});
</ script>
</ body>
</ html>
3. Method summary
1. Configure the corresponding controller and inject $ scope and $ http services into the controller;
2. Use $ http.get () to write the url of the data file to be read;
3. Use the callback function. When successful, assign the resulting data to the variable products under the scope of $ scope.
4. Use the ng-repeat command to traverse the data one by one at the front desk.
Part of the content is reproduced from: Mask Goblin's Blog
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.