javascript - 使用 angularJS 获取数据

标签 javascript mysql angularjs html

我正在尝试使用 angularJS 获取 HTML 表中的数据,我已将项目分为 3 部分:

1- 主要部分:main.html:加载 html 表文件和 Java 脚本(angularJS)(Main.html)

<div class="panel panel-primary">
    <div class="panel-heading">Student's List</div>
    <div ng-include="'Student_list.html'">
</div>
<script src= "../js/ngMain.js"></script>

2- Html Table:它是 Table html 标签 (Student_list.html)

<table class="table table-borAdherentdered table-hover" id="stdlst">
        <thead>
            <tr>
                <th>Id</th>
                <th>Full Name</th>
                <th>Email</th>
                <th>Birth's Date</th>
                <th>Class</th>
            </tr>
        </thead>
        <tbody>
                <tr ng-repeat="std in students">
                    <td>{{std.Id}}</td>
                    <td>{{std.Name}}</td>
                    <td>{{std.Email}}</td>
                    <td>{{std.BoD}}</td>
                    <td>{{std.Class}}</td>
                </tr>
        </tbody>

    </table>

3- JS 文件:从 mySQL (ngMain.js) 加载数据

var app = angular.module('myApp', []);
 app.controller('myCtrl', function($scope,$http) {

    $scope.GetStudent=function() {
        $scope.students=[];
        $http.post("Main.php",{'ScriptName':'GetData','ScriptParam':'student'+"|"+'Id'+";"+'Name'+";"+'Email'+";"+'BoD'+";"+'Class',"DataTypeResult":'string'})
       .success(function (data,status,headers,config) {
        $scope.students=data;
        console.log("Get Student List succesfully");
      });
    }

});

php 脚本(Main.php)正在工作,我已经使用 REST 应用程序进行了测试,没有任何问题。

我的问题在这里: 是不是我没有得到任何数据,只有表头(顺便说一句:在学生表中有很多记录)? 知道缺少什么吗?或者如果我做错了请纠正我。

谢谢。 /库尔

最佳答案

我认为您发布的代码不完整。 根本没有 ng-app 和 ng-controller,并且 GetStudent 函数永远不会被调用。

但是这里有一个带有模拟数据的工作 fiddler ,我认为它会对您有所帮助。

http://jsfiddle.net/ds7hryn5/2/

HTML:

<div ng-app="myApp">
  <div class="panel panel-primary" >
  <div class="panel-heading">Student's List</div>
  <table class="table table-borAdherentdered table-hover" id="stdlst" ng-controller="myCtrl">
       <thead>
         <tr>
            <th>Id</th>
            <th>Full Name</th>
            <th>Email</th>
            <th>Birth's Date</th>
            <th>Class</th>
         </tr>
       </thead>
       <tbody>
            <tr ng-repeat="std in students">
                <td>{{std.Id}}</td>
                <td>{{std.Name}}</td>
                <td>{{std.Email}}</td>
                <td>{{std.BoD}}</td>
                <td>{{std.Class}}</td>
            </tr>
       </tbody>

   </table>
   </div>
</div>

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope','$http',function($scope,$http) {

  $scope.GetStudent=function() {
    $scope.students=[{
        Id:4,
        Name:'Name1',
        Email:'name1@eee.it',
        BoD:'bod1',
        Class:'class'
    },{
        Id:5,
        Name:'Name2',
        Email:'name2@eee.it',
        BoD:'bod2',
        Class:'class'
    },{
        Id:6,
        Name:'Name3',
        Email:'name3@eee.it',
        BoD:'bod3',
        Class:'class'
    }];

  }
  $scope.GetStudent();

}]);

关于javascript - 使用 angularJS 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33363250/

相关文章:

javascript - $watch 没有像预期的那样触发多次

javascript - 需要我的 jQuery 选项卡在事件且已经打开时不关闭

php - 将数组添加到sql数据库中

php - 使用 PHP 和 Mysql 使用一个查询将数据插入 2 个表

css - 如何使 div 文本响应小部件调整大小

c# - .NET 中的 Javascript Date.UTC() 模拟

javascript - Webpack 将代码拆分为通过 npm 包含的单独模块,如何编译 es6?

php - 我应该在 MySQL 中使用什么数据类型来存储 XSD 格式的日期时间?

javascript - 如果图片尺寸过大,则阻止上传

angularjs - Angular 应用程序中的 Google API 服务帐户