简体   繁体   English

AngularJS-针对同一表单多次httpget

[英]AngularJS - httpget multiple times for same form

I am making an application. 我正在申请。 I have a form where the user can type in a product. 我有一个表格,用户可以在其中输入产品。 I get the price with an angularjs service. 我通过angularjs服务获得价格。 I inject it in. RateInfoService. 我将其注入。RateInfoService。

So if I type in "Milk" it will say in the DOM {{rate}} -> $2 因此,如果我输入“牛奶”,它将在DOM {{rate}}-> $ 2中说

But if I fill in chips, it will change offcourse to {{rate}} -> $1.50 但是,如果我填写筹码,它将偏离路线,变为{{rate}}-> $ 1.50

But what I want to fill in Milk and after that Chips. 但是我想在Milk和之后的碎屑中填入什么。 So I want it to show in the html page 所以我希望它显示在html页面中

Milk->$2 & Chips->$1.50. 牛奶-> $ 2;薯片-> 1.50美元。

So it will show both items, the user can keep typing in products and submitting it. 因此它将显示两个项目,用户可以继续输入产品并提交。 (ng-click). (NG-点击)。 I might be able to do that in an array but note that I have a refresh rate . 我也许可以在数组中做到这一点,但请注意,我有刷新率 Not sure if that's possible to do with an ng-repeat or even if that is possible. 不知道这是否与ng-repeat有关,甚至是否可能。

  $scope.submit = function() { 
  $scope.alarmingMarket = $scope.hard
  console.log($scope.monitors);
};

Also some extra info 还有一些额外的信息

  $scope.submit = function() { $scope.alarmingMarket = $scope.hard console.log($scope.monitors); }; 

$scope.alarmingMarket = $scope.hard $ scope.alarmingMarket = $ scope.hard

I used this so it would only get the market price after the user has filled in & pressed submit. 我使用了它,因此只有在用户填写并按下“提交”后,它才会获得市场价格。

RateInfoService to get the price: http://pastebin.com/gHfhzMjR RateInfoService获取价格:http://pastebin.com/gHfhzMjR

I just found out it's possible to dump multiple objects in the url. 我只是发现有可能在URL中转储多个对象。

https://query.private%20in%20("Milk","Chips")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback= HTTPS://query.private%20in%20( “牛奶”, “芯片”)&格式= JSON和诊断=真ENV =商店%3A%2F%2Fdatatables.org%2Falltableswithkeys&回调=

How can I do this the best way in angular? 我怎样才能做到最好的角度? i'm new in angularjs (3 weeks) 我是angularjs的新手(3周)

this is the Json formatted view (Explains the $scope.rate = data.query.results.rate.Rate;) 这是Json格式的视图(解释$ scope.rate = data.query.results.rate.Rate;)

  "results": { "rate": [ { "id": "MILK", "Name": "MILK", "Rate": "1.1054", "Date": "10/26/2015", "Time": "9:37pm", "Ask": "1.1056", "Bid": "1.1052" }, { "id": "CHIPS", "Name": "CHIPS", "Rate": "1.5349", "Date": "10/26/2015", "Time": "9:37pm", "Ask": "1.5352", "Bid": "1.5346" 

I hope this was enough info. 我希望这是足够的信息。 *** Not sure why the styling goes wrong. ***不知道为什么样式会出错。 Sorry for the messy page. 对不起,页面混乱。 Stack overflow gives a good preview of this post, but something goes wrong when I publish it. 堆栈溢出为这篇文章提供了很好的预览,但是当我发布它时出现了问题。 A lot of the code isn't greyed out 许多代码没有变灰

My solution 我的解决方案

    $scope.alarmingMarket = [];
  $scope.submit = function() { 

     // $scope.alarmingMarket = $scope.hard
     $scope.alarmingMarket.push($scope.hard);
      console.log($scope.alarmingMarket);
    };

This to push everything in the $scope.alarmingMarket array. 这会将所有内容推送到$ scope.alarmingMarket数组中。

A service to get the rates 获得费率的服务

    module.service('RateInfoService', function($http) {
        return {
                getMarket: function(alarmingMarket) {
                    var market = alarmingMarket
                        return $http.get('https://query.private%20in%20(%22'+ market  + '%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=')
                }
        }
})

market is the array. 市场是阵列。

and to parse the Json 并解析Json

 $scope.refreshData = function() { 
RateInfoService.getMarket($scope.alarmingMarket).success(function(data) {
  if($scope.alarmingMarket.length <= 1){ //if alarmingMarket[] has only 1 market
    $scope.rate = [];
    $scope.rate.marketid = data.query.results.rate.id;
    $scope.rate.rate = data.query.results.rate.Rate;
    $scope.rate.updateTime = data.query.results.rate.Time;
    console.log($scope.rate);
  }else{
  angular.forEach(data.query.results.rate, function (value, key) {
    $scope.rate = [];
    $scope.rate.marketid = data.query.results.rate[key].id;
    $scope.rate.rate = data.query.results.rate[key].Rate;
    $scope.rate.updateTime = data.query.results.rate[key].Time;
    console.log($scope.rate);
});
  }
})

Sorry don't have enough rep to post a comment. 抱歉,没有足够的代表来发表评论。 Please give me a plus if this helps so I can get more rep. 如果有帮助,请给我加分,这样我就可以获得更多代表。

From what you posted it seems like you are trying to repeat over items selected by the user and then getting each new rate per item (not sure if $scope.alarmingMarket is an array of items or not). 从您发布的内容看来,您似乎想重复用户选择的项目,然后获取每个项目的每个新费率(不确定$ scope.alarmingMarket是否为项目数组)。

If you wanted to get all the latest rates for items selected by the user, you have to pass the entire array of items up and get back an array and let the ng-repeat do the work by assigning the value to the $scope.alarmingMarket (if that was your item list). 如果要获取用户选择的商品的所有最新价格,则必须向上传递整个商品数组并返回一个数组,然后通过将值分配给$ scope.alarmingMarket来让ng-repeat进行工作。 (如果那是您的商品列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM