1. Trang chủ
  2. » Thể loại khác

Ajax trong AngularJS | 26 bài hướng dẫn AngularJs miễn phí PDF

4 175 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 4
Dung lượng 324,45 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Ajax trong AngularJS AngularJS cung cấp điều khiển $http mà làm như là service để đọc dữ liệu từ server.. Server có thể tạo database call để nhận các bản ghi.. AngularJS cần dữ liệu dưới

Trang 1

Ajax trong AngularJS AngularJS cung cấp điều khiển $http mà làm như là service để đọc dữ liệu từ server Server có thể tạo database call để nhận các bản ghi AngularJS cần dữ liệu dưới định dạng JSON Khi dữ liệu sẵn sàng, $http có thể được sử dụng để nhận dữ liệu từ server theo cách sau đây:

function studentController($scope,$http) {

var url="data.txt";

$http.get(url).success( function(response) {

$scope.students = response;

});

}

Ở đây tệp data.txt chứa các bản ghi về student $http service tạo một ajax call và lấy kết quả trả về

cho đối tượng student “students” model có thể dùng để vẽ bảng với HTML

Các ví dụ

data.txt

[

{

"Name" "Mahesh Parashar",

"RollNo" 101,

"Percentage" "80%"

},

{

"Name" "Dinkar Kad",

"RollNo" 201,

"Percentage" "70%"

},

{

"Name" "Robert",

"RollNo" 191,

"Percentage" "75%"

},

Trang 2

"Name" "Julian Joe",

"RollNo" 111,

"Percentage" "77%"

}

]

testAngularJS.jsp

<html>

<head>

<title>Angular JS Includes</title>

<style>

table, th , td {

border: 1px solid grey;

border-collapse: collapse;

padding: 5px;

}

table tr:nth-child(odd) {

background-color: #f2f2f2;

}

table tr:nth-child(even) {

background-color: #ffffff;

}

</style>

</head>

<body>

<h2>AngularJS Sample Application</h2>

<div ng-app="" ng-controller="studentController">

<table>

<tr>

<th>Name</th>

<th>Roll No</th>

<th>Percentage</th>

Trang 3

</tr>

<tr ng-repeat="student in students">

<td>{{ student.Name }}</td>

<td>{{ student.RollNo }}</td>

<td>{{ student.Percentage }}</td>

</tr>

</table>

</div>

<script>

function studentController($scope,$http) {

var url="data.txt";

$http.get(url).success( function(response) {

$scope.students = response;

});

}

</script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>

</body>

</html>

K ết quả

Để chạy ví dụ này, bạn cần tạo testAngularJS.jsp and data.txt tới một webserver Mở tệptestAngularJS.jsp với địa chỉ URL trên server trên một trình duyệt web và xem kết quả

Ngày đăng: 02/12/2017, 19:03

TỪ KHÓA LIÊN QUAN