背景:由于长期从事于后端开发,对界面的绘制功底特别薄弱,刚好今天早上去博客园浏览文章的时候,看到推送消息是学习AngularJS,由于带着好奇心果断的点开看了一下,早期的时候听说过前端三大框架,但是都没有真正的使用过,今天就尝试一下,通过后端构建好服务之后,然后使用AngularJS去调用,最后渲染视图,服务用的WebApi
服务端数据模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace _01___请求服务获取数据.Models
{
public class Student
{
public string StudentNo { get; set; }
public string StudentName { get; set; }
public int StudentAge { get; set; }
public string StudentAddress { get; set; }
public DateTime StudentBirthDay { get; set; }
}
}
服务端返回数据接口:
using _01___请求服务获取数据.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace _01___请求服务获取数据.Controllers
{
public class StudentController : ApiController
{
[Route("Rotu/GetStudentList")]
[HttpGet]
public List<Student> StudentList()
{
return new List<Student>()
{
new Student()
{
StudentNo ="001",
StudentName ="Rotu",
StudentAddress ="HB",
StudentAge=18,
StudentBirthDay=DateTime.Now
},
new Student()
{
StudentNo ="002",
StudentName ="Jone",
StudentAddress ="HB",
StudentAge=18,
StudentBirthDay=DateTime.Now
},
new Student()
{
StudentNo ="003",
StudentName ="Andy",
StudentAddress ="HB",
StudentAge=18,
StudentBirthDay=DateTime.Now
},
new Student()
{
StudentNo ="004",
StudentName ="Jack",
StudentAddress ="HB",
StudentAge=18,
StudentBirthDay=DateTime.Now
},
};
}
}
}
页面请求:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
tr {
text-align:center;
}
</style>
<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="MyApp" ng-controller="MyController">
<table ng-if="data!=null" border="1">
<tr>
<td>编号</td>
<td>姓名</td>
<td>住址</td>
<td>生日</td>
<td>年龄</td>
</tr>
<tr ng-repeat="x in data">
<td>{{x.StudentNo}}</td>
<td>{{x.StudentName}}</td>
<td>{{x.StudentAddress}}</td>
<td>{{x.StudentBirthDay|date:'yyyy-MM-dd HH:mm:ss'}}</td>
<td>{{x.StudentAge}}</td>
</tr>
</table>
</div>
<script>
var app = angular.module("MyApp", []);
app.controller("MyController", function ($scope, $http)
{
var requestUrl = "/Rotu/GetStudentList";
$http.get(requestUrl).then(function (response) {
$scope.data = response.data;
});
});
</script>
</body>
</html>
最终效果: