javascript - C# Web Api 获取方法

标签 javascript c# angularjs asp.net-web-api

这是我的模型文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ConsumeJson.Models
{
    public class ProductModel
    {
        public List<Product> findAll()
        {
            List<Product> result = new List<Product>();
            result.Add(new Product { Id = "p01", Name = "Product 1", Price = "100", Quantity = "1" });
            result.Add(new Product { Id = "p02", Name = "Product 2", Price = "200", Quantity = "2" });
            result.Add(new Product { Id = "p03", Name = "Product 3", Price = "300", Quantity = "3" });
            return result;
        }
        public Product find(string id)
        {
            return findAll().Single(p => p.Id.Equals(id));
        }
    }
}

这是我的 HTML 文件。

<!DOCTYPE html>
<html ng-app="myapp">
<head>
    <meta charset="utf-8" />
    <script src="Scripts/jquery-3.3.1.min.js"></script>
    <script src="Scripts/jquery-3.3.1.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
    <title>My Client</title>
</head>
<body ng-controller="productController">

    <table cellpadding="2" cellspacing="2" border="1">
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
        <tr ng-repeat="product in result">
            <td>{{product.Id}}</td>
            <td>{{product.Name}}</td>
        </tr>
    </table>

    <script type="text/javascript">
        var myapp = angular.module('myapp', []);
        myapp.controller('productController', function ($scope, $http) {
            $http(
                method: 'GET',
                url:'http://localhost:53204/api/product').success(function (response) {
                $scope.result = response;
            })
        })
    </script>
</body>
</html>

我想创建包含信息的产品表,但是当我作为本地主机运行它时,它永远不会显示产品的 ID 或名称。 它保持这样。 {{Product.Id}} {{Product.Name}} 我该如何解决这个问题?

最佳答案

您需要将其更改为使用.then()

$http({
    method: "GET",
    url: "http://localhost:53204/api/product"
}).then(function mySucces(response) {
    $scope.result = response.data;
}, function myError(response) {

});

关于javascript - C# Web Api 获取方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51460898/

相关文章:

c# - C# 中如何确定变量类型是否支持给定运算符

angularjs - 无法通过 npm 安装 gulp-jshint

angularjs - 在 AngularJS 中以 RESTful 方式设置路由的最佳实践

javascript - 身份验证后应在 Flux 应用程序中的何处进行存储重新获取操作?

javascript - 引用错误: "sheet" is not defined

c# - 为什么 "is"-operator 在 if 的范围比 if 更长?

c# - WPF - 因值更改而触发的常见事件

javascript - AngularJS 在 ng-repeat 中更改变量

javascript - 文本框的背景颜色

javascript - 如何用jquery控制图片扩展?