【问题标题】:Consuming web api using angular js with c#使用 angular js 和 c# 使用 web api
【发布时间】:2018-11-04 22:10:58
【问题描述】:

我想使用 AngularJs 使用 web api 来使用 c# 创建仪表板。我成功使用了 web api,但是当我想添加 angular Js 来创建我的仪表板时,什么也没有出现。我不知道是什么问题。

脚本.js

var app = angular.module("DemandeApp", []); app.controller("DemCreditController", function ($scope, $http) {
        $http.get("http://localhost:2573/api/demandes")
         .then(function (response) {
             $scope.labels = [];
             $scope.data = [];
             angular.forEach(response.data, function (value, key) {
                 $scope.labels[key] = value.libelle;
                 $scope.data[key] = value.quantite;
             }
         );

         }); 

 }); 

index.html:

<body ng-app="DemandeApp">
    <div ng-controller="DemCreditController">

        <canvas id="pie" class="chart chart-pie"
                chart-data="data" chart-labels="labels" ></canvas> 
</div></body>

【问题讨论】:

  • 你需要将数据推送到数组中

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


【解决方案1】:

您可以尝试像下面的代码将数据推送到您的数组,

$http.get("http://localhost:2573/api/demandes")
   .then(function (response) {
       $scope.labels = [];
       $scope.data = [];
       angular.forEach(response.data, function (value, key) {
          $scope.labels.push(value.libelle);
          $scope.data.push(value.quantite);
       }
);

【讨论】:

  • 看来您不需要密钥来将值推送到 $scope.data 数组。我正在相应地更新答案,请试一试。
猜你喜欢
  • 2016-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-04-16
  • 2018-01-22
  • 2018-01-25
相关资源
最近更新 更多