扫码一下
查看教程更方便
angularjs ajax是通过提供 $http 控件来实现的,它可以从服务器读取数据。angularjs 需要 json 格式的数据。数据准备好后,可以使用 $http 通过以下方式从服务器获取数据
function studentcontroller($scope,$https:) {
var url = "/demo_source/angularjs/data.txt";
$https:.get(url).success( function(response) {
$scope.students = response;
});
}
这里,文件 data.txt 包含学生记录。$http 服务进行 ajax 调用并设置对其属性 students 的响应。students 模型可用于在 html 中绘制表格。
data.txt
[ { "name" : "mahesh parashar", "rollno" : 101, "percentage" : "80%" }, { "name" : "dinkar kad", "rollno" : 201, "percentage" : "70%" }, { "name" : "robert", "rollno" : 191, "percentage" : "75%" }, { "name" : "julian joe", "rollno" : 111, "percentage" : "77%" } ]
从服务器获取数据代码如下
angularjs sample application
name
roll no
percentage
{{ student.name }}
{{ student.rollno }}
{{ student.percentage }}