[C#]为什么 angular 不绑定中的数据ng-model

发布时间: 2017/2/25 7:03:18
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在这里我得到数据的数据库,我也 trace 从提琴手,但当我试图在数据绑定数据 angular 表它不具约束力

Linq 查询

public JsonResult GetSalesData()
        {
            using (Ctxdb dc = new Ctxdb())
            {


                var v = (from a in dc.SalesRecords
                         group a by a.SalesDate.Year into g
                         select new
                         {
                             Year = g.Key,
                             Electronics = g.Sum(a => a.Electronics),
                             BookAndMedia = g.Sum(a => a.BookAndMedia),
                             HomeAndKitchen = g.Sum(a => a.HomeAndKitchen)
                         });
                if (v != null)
                {
                    var chartData = new object[v.Count() + 1];
                    chartData[0] = new object[]{
                "Year",
                "Electronics",
                "Book and Media",
                "Home and KitchenKFC"
            };
                    int j = 0;
                    foreach (var i in v)
                    {
                        j++;
                        chartData[j] = new object[] { i.Year.ToString(), i.Electronics, i.BookAndMedia, i.HomeAndKitchen };}
 return new JsonResult { Data = chartData, JsonRequestBehavior = JsonRequestBehavior.AllowGet };}}
return new JsonResult { Data = null, JsonRequestBehavior = JsonRequestBehavior.AllowGet };}

Fidller 数据

[["Year","Electronics","Book and Media","Home and KitchenKFC"],["1947",99489,46000,26833],["1995",25555,45000,99000],["2011",1584,5685,5566],["2012",12121,1500,1200],["2014",600,5132,2730],["2015",1789,1255,1258]]

AngularService

app.service('MyGraphService', function ($http) {
    this.GetJanu = function () {
        alert('in Service')

        var Sert = $http({
            url: '/Department/GetSalesData',
            method: 'GET',
            data: JSON.stringify(),
            content:{'content-type' :'application/Json'}
        })
        return Sert;
    }
})

Angular控制器

app.controller('GraphCntrls', function ($scope, MyGraphService) {
    $scope.Btnclick = function () {
        alert('in cntr')
        var tt = MyGraphService.GetJanu();
        tt.then(function (d) {
            $scope.DataGraphf = d.data;
        })
    }
})

Cshtml 代码

<div>

        <div ng-controller="GraphCntrls">


            <input type="button" ng-click="Btnclick()" value="Click"  class="btn btn-success btn-sm"/>


            <table class="table table-striped table-bordered">
                <tr>
                    <th><b>Year</b></th>

                    <th><b>Electronics</b></th>
                    <th><b>Book and Media</b></th>
                    <th><b>Home and KitchenKFC</b></th>

                </tr>
                @*<tr ng-repeat="datf in DataGraphf">*@
                <tr>
                    <td>{{Year}}</td>
                    <td>{{datf.Electronics}}</td>
                    <td>{{datf.BookAndMedia}}</td>
                    <td>{{datf.HomeAndKitchen}}</td>

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

解决方法 1:

将表行更改为︰

<tr ng-repeat="datf in DataGraphf" ng-if="!$first">
  <td>{{datf[0]}}</td>
  <td>{{datf[1]}}</td>
  <td>{{datf[2]}}</td>
  <td>{{datf[3]}}</td>
</tr>

因为你正在返回数组的数组,并在表格标题的第一个索引。您想要隐藏的第一个结果 elementng-if="!$first" 。 然后你后续的元素是具有 4 值数组,所以你必须使用数组索引来访问属性 vs 对象名称。

使用对象名称,您必须返回一个对象而不是数组,例如

{
  "Year": 1230,
  "Electronics": 123,
  "BookAndMedia" : 1234,
  "HomeAndKitche" : 515
}

响应 json 将是︰

[{...},{...},{...}]

更新︰ 有 extra tr tag 在原始响应,转移 ng-if 到 tr 相反

赞助商