求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 
 

AngularJS教程
AngularJS快速入门
AngularJS环境设置
AngularJS MVC体系结构
AngularJS第一个应用程序
AngularJS指令
AngularJS表达式
AngularJS控制器
AngularJS过滤器
AngularJS表格
AngularJS HTML DOM
AngularJS模块
AngularJS表单
AngularJS包括
AngularJS Ajax
AngularJS视
AngularJS作用域
AngularJS服务
AngularJS依赖注入
AngularJS自定义指令
AngularJS国际化
 
 

AngularJS Ajax
743 次浏览
34次  

AngularJS提供$http控制,可以作为一项服务从服务器读取数据。服务器可以使一个数据库调用来获取记录。 AngularJS需要JSON格式的数据。一旦数据准备好,$http可以用以下面的方式从服务器得到数据。

function studentController($scope,$http) {
    var url="data.txt";
        $http.get(url).success( function(response) {
                               $scope.students = response;
                             });
      }

例子

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%"
    }
    ]

testAngularJS.html

<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>  
     </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>

输出

要要运行这个例子,需要部署textAngularJS.html,data.txt到一个网络服务器。使用URL在Web浏览器中打开textAngularJS.html请求服务器。看到结果如下:


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



743 次浏览
34次
 捐助