【问题标题】:AngularJS with RESTful MySQLAngularJS 与 RESTful MySQL
【发布时间】:2017-01-02 11:53:06
【问题描述】:

我使用 AngularJS 和使用 MySQL 数据库的 RESTful WebService。我从数据库中获取数据以查看页面。但我的问题是:我无法对函数求和。

问题代码:

$scope.currentAssetsSum = function () {

//return parseFloat($scope.currentAssetsData.cash_hand) + parseFloat($scope.currentAssetsData.bank_account);

return $scope.currentAssetsData['cash_hand'] + $scope.currentAssetsData['bank_account'];

};

JAR List:


MySQL 数据库:

    CREATE TABLE  current_assets (
id int(10) unsigned NOT NULL auto_increment, 
cash_hand double NOT NULL, 
bank_account double NOT NULL, 
PRIMARY KEY  (id));

NetBeans Web 应用程序项目:

https://github.com/hellodewdrop/AngularJS_RESTful_MySQL

【问题讨论】:

  • 你需要确保你的函数在对它们求和时返回一个 int 或 float。
  • @HRgiger:没有必要在所有正在使用的技术中添加粗体——这使得它更难阅读。只需确保 OP 的情况正确(例如 MySQL 而不是 MYSQL)。
  • @halfer 指出:)

标签: java mysql angularjs rest


【解决方案1】:

您之前是否将变量 $scope.currentAssetsData 声明为对象?然后你可以进行转换,如果你的数据不是浮点数。

类似:

var app = angular.module("myApp", []);
        app.controller("myCtrl", function ($scope, $http) {
           $scope.currentAssetsData = {};

            //Current Assets Table
            $http({
                method: "GET",
                url: "http://localhost:8084/Final_Balance/rest/currentAssets"
            }).then(function mySucces(response) {
                $scope.currentAssetsData = response.data;
                console.log($scope.currentAssetsData.length);
            }, function myError(response) {
                console.log("Data can't be loaded..");
            });
            //Calculation from Table
            $scope.currentAssetsSum = function () {
                return parseFloat($scope.currentAssetsData.cash_hand) + parseFloat($scope.currentAssetsData.bank_account);
            };
        });

【讨论】:

  • 我已经根据你的代码更新了我的答案。尝试将 index.html 中的控制器代码替换为编辑后的代码,看看它是否有效!
  • 之前返回的是哪个结果?
  • 我想返回从数据库中获取的两个字段值的总和。它工作正常:{{item.bank_account + item.cash_hand}}。但没有在功能上求和。兄弟,在github上我的netbeans项目。请运行它然后你就有问题了。
  • 我下载了项目,当我在 chrome 上按 F12 时,它说:“无法读取未定义的属性 'cash_hand'”,这意味着您的 $scope.currentAssetsData 未声明。如果在我传递给你的代码中结果是 NaN,可能意味着你的 API 没有返回正确的数据,或者没有返回数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-12
  • 2015-10-14
  • 2015-07-08
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多