背景:由于长期从事于后端开发,对界面的绘制功底特别薄弱,刚好今天早上去博客园浏览文章的时候,看到推送消息是学习AngularJS,由于带着好奇心果断的点开看了一下,早期的时候听说过前端三大框架,但是都没有真正的使用过,今天就尝试一下,通过后端构建好服务之后,然后使用AngularJS去调用,最后渲染视图,服务用的WebApi

    服务端数据模型:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Web;
    5. namespace _01___请求服务获取数据.Models
    6. {
    7. public class Student
    8. {
    9. public string StudentNo { get; set; }
    10. public string StudentName { get; set; }
    11. public int StudentAge { get; set; }
    12. public string StudentAddress { get; set; }
    13. public DateTime StudentBirthDay { get; set; }
    14. }
    15. }

    服务端返回数据接口:

    1. using _01___请求服务获取数据.Models;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using System.Net;
    6. using System.Net.Http;
    7. using System.Web.Http;
    8. namespace _01___请求服务获取数据.Controllers
    9. {
    10. public class StudentController : ApiController
    11. {
    12. [Route("Rotu/GetStudentList")]
    13. [HttpGet]
    14. public List<Student> StudentList()
    15. {
    16. return new List<Student>()
    17. {
    18. new Student()
    19. {
    20. StudentNo ="001",
    21. StudentName ="Rotu",
    22. StudentAddress ="HB",
    23. StudentAge=18,
    24. StudentBirthDay=DateTime.Now
    25. },
    26. new Student()
    27. {
    28. StudentNo ="002",
    29. StudentName ="Jone",
    30. StudentAddress ="HB",
    31. StudentAge=18,
    32. StudentBirthDay=DateTime.Now
    33. },
    34. new Student()
    35. {
    36. StudentNo ="003",
    37. StudentName ="Andy",
    38. StudentAddress ="HB",
    39. StudentAge=18,
    40. StudentBirthDay=DateTime.Now
    41. },
    42. new Student()
    43. {
    44. StudentNo ="004",
    45. StudentName ="Jack",
    46. StudentAddress ="HB",
    47. StudentAge=18,
    48. StudentBirthDay=DateTime.Now
    49. },
    50. };
    51. }
    52. }
    53. }

    页面请求:

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8" />
    5. <title></title>
    6. <style>
    7. tr {
    8. text-align:center;
    9. }
    10. </style>
    11. <script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
    12. </head>
    13. <body>
    14. <div ng-app="MyApp" ng-controller="MyController">
    15. <table ng-if="data!=null" border="1">
    16. <tr>
    17. <td>编号</td>
    18. <td>姓名</td>
    19. <td>住址</td>
    20. <td>生日</td>
    21. <td>年龄</td>
    22. </tr>
    23. <tr ng-repeat="x in data">
    24. <td>{{x.StudentNo}}</td>
    25. <td>{{x.StudentName}}</td>
    26. <td>{{x.StudentAddress}}</td>
    27. <td>{{x.StudentBirthDay|date:'yyyy-MM-dd HH:mm:ss'}}</td>
    28. <td>{{x.StudentAge}}</td>
    29. </tr>
    30. </table>
    31. </div>
    32. <script>
    33. var app = angular.module("MyApp", []);
    34. app.controller("MyController", function ($scope, $http)
    35. {
    36. var requestUrl = "/Rotu/GetStudentList";
    37. $http.get(requestUrl).then(function (response) {
    38. $scope.data = response.data;
    39. });
    40. });
    41. </script>
    42. </body>
    43. </html>

    最终效果:

    webwxgetmsgimg.jpg