【问题标题】:Display multiple Oracle SQL query results on AngularJS page在 AngularJS 页面上显示多个 Oracle SQL 查询结果
【发布时间】:2018-05-09 13:39:59
【问题描述】:

所以我需要在AngularJS的同一个组件上显示多个SQL查询的结果。

我该怎么做?到目前为止,我了解到组件只能处理一个 http 请求,如下所示:

'use strict';
angular.module('cryostat', []).component('cryostat', {
    templateUrl: 'cryostat/cryostat.template.html',
    controller: function cryostatController($http) {
        this.pageTitle = "NP04 Cryostat"
        this.natalie = 1;
        $http.get("cryostat.conn.php")
           .then(function (response) {this.TT0101 = response.data.records;});
    }
});

【问题讨论】:

  • 你用多个请求填充了什么?

标签: sql angularjs oracle webpage


【解决方案1】:

我知道组件只能处理一个http请求

这根本不是真的。你是从哪里听来的?用$q.all()同时发出两个请求很容易:

var promiseOne = $http.get("query1.php");
var promiseTwo = $http.get("query2.php");

$q.all([promiseOne, promiseTwo]).then(function(resultArray) {
  $scope.resultOne = resultArray[0];
  $scope.resultTwo = resultArray[1];
});

【讨论】:

  • 感谢您的回复。我已经尝试过了,但是我在 IDE 中对 $q.all 提出了以下建议:缺少导入语句和 required() 丢失
  • 我在代码中打错了字。调用.then() 需要function 关键字。此外,您需要以与注入 $http 相同的方式注入 $q 依赖项
  • 我就是这么想的。谢谢!但是现在在运行页面时,我在浏览器 DevTools 中得到“[$http:badreq] Http 请求配置必须是一个对象。收到:cryostat.conn.php”。这可能是因为我没有安装服务器并且没有执行 PHP 代码吗?
  • 调用$http.get时,字符串参数应该是一个完整的URL。这是一个人为的例子。但是如何使用$http已经在别处回答了
  • 如果我使用的不是 $scope 而是控制器怎么办?
【解决方案2】:

为您的目的使用$q.all()

var myData_One, myData_two;
var promises = [];
function promiseA() {
    let deferred = $q.defer();

    ajaxCall().then((response) => {
        $scope.myData_One = response;
        deferred.resolve();
    }, (error) => {
        deferred.reject(error);
    });

    return deferred.promise;
}
function promiseB() {
    let deferred = $q.defer();

    ajaxCall().then((response) => {
        $scope.myData_two = response;
        deferred.resolve(response);
    }, (error) => {
        deferred.reject(error);
    });

    return deferred.promise;
}
var promises = [promiseA(), promiseB()];
$q.all(promises).then((values) => {
    //Do whatever you wish to with response data from both promises.
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2019-09-14
    • 2016-08-04
    • 2014-08-24
    • 1970-01-01
    相关资源
    最近更新 更多