Angularjs 从多个web api方法获取JSON数据($q.all)

Angularjs 从多个web api方法获取JSON数据($q.all),angularjs,asp.net-web-api,angular-promise,angularjs-http,Angularjs,Asp.net Web Api,Angular Promise,Angularjs Http,我有一个带有表单的简单应用程序。加载表单时,我想调用两个web api方法来获取用于初始化表单的json数据。在我的工厂类中,表单与硬编码数据配合良好。我不确定如何在该文件中发出多个请求,并且有点卡住了 表格: <div class="modal-header" style="text-align:center"> <h3 class="modal-title">Configure</h3> <div style="margin-top:10px">

我有一个带有表单的简单应用程序。加载表单时,我想调用两个web api方法来获取用于初始化表单的json数据。在我的工厂类中,表单与硬编码数据配合良好。我不确定如何在该文件中发出多个请求,并且有点卡住了

表格:

<div class="modal-header" style="text-align:center">
<h3 class="modal-title">Configure</h3>
<div style="margin-top:10px">
    <button tabindex="100" class="btn btn-success pull-left" type="submit">Submit</button>
    <button class="btn btn-warning pull-right" ng-click="close($event)">Close</button>
    </div>
</div>
<div class="modal-body">
<div class="col-sm-6" style="width: 100%;">
    <form name="joinForm">
        <div class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-sm-3">Symbol</label>
                <div class="col-sm-9">
                    <select ng-model="simulationsettings.symbols" ng-  options="key as value for (key,value) in symbols"></select>
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-sm-3">Interval</label>
                <div class="col-sm-9">
                    <select ng-model="simulationsettings.intervals" ng-options="key as value for (key,value) in intervals"></select>
                </div>
            </div>
        </div>
    </form>
</div>
保存表单(硬编码)数据的factory类:

mainApp2.factory("moduleConfigformService",
function () {
    return {
        simulationsettings: {
            symbols: {
                'symbol1': "symbol1",
                'symbol2': "symbol2"
            },
            intervals: {
                '60': "1 minute",
                '120': "2 minutes",
                '180': "3 minutes"
            }
        }
    }
});
我想调用服务器,而不是硬编码的值,但经过几个小时的研究、跟踪和出错后,它被卡住了:

mainApp2.factory("moduleConfigformService",
function () {

    function getSymbols() {
        return $http.get("/api/getsymbols");
    }

    function getIntervals() {
        return $http.get("/api/getIntervals");
    }
    return {
        simulationsettings: {
            symbols : getSymbols()
            },
            intervals : getIntervals()
    }
});

你能给我指出正确的方向吗?

如果我看一下你的控制器,我看不出你在等待http请求返回答案

你应该看看 简言之,你必须使用类似

moduleConfigformService.simulationsettings.then(function (response) {
//doSomeThingWithResponse
})

如果我看一下您的控制器,我看不出您在等待http请求返回答案

你应该看看 简言之,你必须使用类似

moduleConfigformService.simulationsettings.then(function (response) {
//doSomeThingWithResponse
})

$http服务不返回值。它回报承诺。看见另外,从工厂使用基于承诺的API可能有点棘手。我建议在控制器中编写和调试代码,然后重新考虑使用工厂

app.factory("moduleConfigformService",
function ($q,$http) {

    function getSymbols() {
        return $http.get("/api/getsymbols")
          .then(function (response) { 
            return response.data;
        });
    }

    function getIntervals() {
        return $http.get("/api/getIntervals")
          .then(function (response) {
            return response.data
        });
    }

    return {
        getSymbols: getSymbols,
        getIntervals: getIntervals,
        simulationsettings: function () {
            var promiseHash = {};
            promiseHash.symbols = getSymbols();
            promiseHash.intervals = getIntervals();
            return $q.all(promiseHash);
        }
    }
});
用法
$http服务不返回值。它回报承诺。看见另外,从工厂使用基于承诺的API可能有点棘手。我建议在控制器中编写和调试代码,然后重新考虑使用工厂

app.factory("moduleConfigformService",
function ($q,$http) {

    function getSymbols() {
        return $http.get("/api/getsymbols")
          .then(function (response) { 
            return response.data;
        });
    }

    function getIntervals() {
        return $http.get("/api/getIntervals")
          .then(function (response) {
            return response.data
        });
    }

    return {
        getSymbols: getSymbols,
        getIntervals: getIntervals,
        simulationsettings: function () {
            var promiseHash = {};
            promiseHash.symbols = getSymbols();
            promiseHash.intervals = getIntervals();
            return $q.all(promiseHash);
        }
    }
});
用法
谢谢。我来看看。谢谢。我来看看。
$http
服务不返回值。它回报承诺。看见另外,从工厂使用基于承诺的API可能有点棘手。我建议在控制器中编写和调试代码,然后重新使用工厂。
$http
服务不返回值。它回报承诺。看见另外,从工厂使用基于承诺的API可能有点棘手。我建议在控制器中编写和调试代码,然后重新考虑使用工厂。现在我只需要弄清楚为什么我的dropdownlist包含[object object],但这是一个新问题:-)现在我只需要弄清楚为什么我的dropdownlist包含[object object],但这是一个新问题:-)