MVC 4中Async和Angular.js使用

类图:

这里写图片描述

实体类

Categories.cs

namespace MvcApplication2.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Categories
    {
        public Categories()
        {
            this.Products = new HashSet<Products>();
        }

        public int CategoryID { get; set; }
        public string CategoryName { get; set; }
        public string Description { get; set; }
        public byte[] Picture { get; set; }

        public virtual ICollection<Products> Products { get; set; }
    }
}

Products.cs

namespace MvcApplication2.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Products
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public Nullable<int> SupplierID { get; set; }
        public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        public bool Discontinued { get; set; }

        public virtual Categories Categories { get; set; }
    }
}

ProductsController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.Entity;
using System.Threading.Tasks;

namespace MvcApplication2.Controllers
{
    [RoutePrefix("api/products")]
    public class ProductsController : ApiController
    {
        Models.NorthwindEntities entity = new Models.NorthwindEntities();

        [HttpGet]
        [Route("categories")]
        public async Task<List<Models.Categories>> Get()
        {
            return await entity.Categories.ToListAsync<Models.Categories>();
        }

        [HttpGet]
        [Route("categories/{id}")]
        public async Task<Models.Categories> GetByID(int id)
        {
            await Task.Delay(5000);
            return await entity.Categories.Include("Products").FirstAsync(x => x.CategoryID == id);
        }
    }
}

index.html

<html>
<head>

</head>
<body>
    <script src="node_modules/angular/angular.js"></script>
    <div ng-app="myApp" ng-controller="myCtrl">
        <div ng-repeat="category in categories">
            <h3>{{category.CategoryName}}</h3>
            <div ng-repeat="product in category.Products">
                <span>{{product.ProductName}}</span>
            </div>
        </div>
    </div>
    <div id="timeresult">


    </div>
    <script src="script.js"></script>
</body>
</html>

script.js

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $http({
        method: 'GET',
        url: 'http://localhost:62801/api/products/categories'
    }).then(function successCallback(response) {
        $scope.categories = response.data;
        document.getElementById("timeresult").innerHTML += new Date().getSeconds() + "<br/>"
        angular.forEach($scope.categories, function(category) {
            setTimeout(function() {
              $http({
                  method: 'GET',
                  url: 'http://localhost:62801/api/products/categories/' + category.CategoryID
              }).then(function successCallback(response) {
                  document.getElementById("timeresult").innerHTML += new Date().getSeconds() + "<br/>";
                  category.Products = response.data.Products;
              }, function errorCallback(response) {});
            }, 1000);

        }); 

    }, function errorCallback(response) {});
});

运行结果如图:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值