0

我是新角度。我正在製作英雄聯盟的應用程序。這不是什麼新東西,而是一個概念證明。從AngularJS數據(英雄聯盟)不能NG重複數組

文件

指數

  <body> 

       <div ng-controller="SummonerController"> 

       <form class="well form-search"> 
        <label>Search:</label> 
        <input type="text" ng-model="keywords" class="input-medium search-query" placeholder="Keywords..."> 
        <button type="submit" class="btn" ng-click="search()">Search</button> 
        <p class="help-block">Type in a LeagueofLegends.com Champion Name.</p>  
       </form> 

       <pre ng-model="result"> 
       <h1>{{summoner.name.data}} </h1> 
       <h2>Level: {{summoner.level}} </h2> 

       <ul> 
       <li ng-repeat="matches in data">{{matches}}</li> 
       </ul> 

       </pre> 

       </div> 

      </body> 
      </html> 

Controllers.js '使用嚴格的';

 angular.module('myApp', []) 
      .controller('SummonerController', function SearchCtrl($scope, $http) { 

      $scope.url = 'process.php'; // The url of our search 

      // The function that will be executed on button click (ng-click="search()") 
      $scope.search = function() { 

       // Create the http post request 
       // the data holds the keywords 
       // The request is a JSON request. 
       $http.post($scope.url, { "data" : $scope.keywords}). 
       success(function(data, status) { 
        $scope.status = status; 
        $scope.data = data; 

        // Show result from server in our <pre></pre> element 
        $scope.result = data; 
        $scope.summoner = data.summoner; 
        $scope.matches = data.matches.array; 
       }) 
       . 
       error(function(data, status) { 
        $scope.data = data || "Request failed"; 
        $scope.status = status;   
       }); 
      }; 

      }); 

process.php

  <?php 
      // The request is a JSON request. 
      // We must read the input. 
      // $_POST or $_GET will not work! 

      $apiKey = "71e73084-6c8e-44a3-b46e-9bc929cd70af"; 

      $data = file_get_contents("php://input"); 
      $objData = json_decode($data); 
      $url = 'https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/' . $objData->data . '?api_key=' . $apiKey; 

      $array = array("summoner" => array('level' => '', 'name' => ''), "matches" => array()); 

      $data = curl_download($url); 
      $var = json_decode($data, true); 

      /* Set X for Array Loop */ 
      $x = 0; 

      foreach($var as $key => $summoner) { 
       foreach($summoner as $item => $value) { 
        if($item == "summonerLevel") 
          $array["summoner"]["level"] = $value; 
          $array["summoner"]["name"] = $objData; 


         if($item == "id") { 
         $id = $value; 
         $matchDownload = curl_download("https://na.api.pvp.net/api/lol/NA/v1.3/game/by-summoner/" . $id . '/recent?api_key=' . $apiKey); 
         $matchData = json_decode($matchDownload, true); 

         foreach($matchData["games"] as $item => $game) { 

          if(!empty($champ['winner'])) { 
          $array["matches"][$x]["winner"] = "Won"; 
          } else { 
          $array["matches"][$x]["winner"] = "Lost"; 
          } 


          $array["matches"][$x]["mode"] = $game['gameMode']; 
          $array["matches"][$x]["type"] = $game['gameType']; 
          $array["matches"][$x]["subtype"] = $game['subType']; 
          $array["matches"][$x]["createdate"] = $game['createDate']; 
          $array["matches"][$x]["ipearned"] = $game['ipEarned']; 

          /* Each Game's Champ */ 
          $champdata = curl_download("https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/" . $game['championId'] . '?api_key=' . $apiKey); 
          $champ = json_decode($champdata, true); 

          $array["matches"][$x]["champion"]["name"] = $champ["name"]; 
          $array["matches"][$x]["champion"]["title"] = $champ["title"]; 
          $array["matches"][$x]["champion"]["level"] = $summoner["summonerLevel"]; 
          $array["matches"][$x]["champion"]["id"] = $champ["id"]; 
          $x++; 
         } 
        } 
       } 
      } 
      $return = json_encode($array); 
      echo $return; 
      ?> 

數據退回

   [ 
        { 
         "winner":"Lost", 
         "mode":"CLASSIC", 
         "type":"MATCHED_GAME", 
         "subtype":"RANKED_SOLO_5x5", 
         "createdate":1440261853283, 
         "ipearned":0, 
         "champion":{ 
         "name":"Jinx", 
         "title":"the Loose Cannon", 
         "level":30, 
         "id":222 
         } 
        }, 
        { 
         "winner":"Lost", 
         "mode":"CLASSIC", 
         "type":"MATCHED_GAME", 
         "subtype":"RANKED_SOLO_5x5", 
         "createdate":1425360797949, 
         "ipearned":51, 
         "champion":{ 
         "name":"Jinx", 
         "title":"the Loose Cannon", 
         "level":30, 
         "id":222 
         } 
        }, 
        { 
         "winner":"Lost", 
         "mode":"CLASSIC", 
         "type":"MATCHED_GAME", 
         "subtype":"RANKED_SOLO_5x5", 
         "createdate":1425185899166, 
         "ipearned":77, 
         "champion":{ 
         "name":"Jinx", 
         "title":"the Loose Cannon", 
         "level":30, 
         "id":222 
         } 
        }, 
        { 
         "winner":"Lost", 
         "mode":"CLASSIC", 
         "type":"MATCHED_GAME", 
         "subtype":"NORMAL", 
         "createdate":1409209618048, 
         "ipearned":62, 
         "champion":{ 
         "name":"Jinx", 
         "title":"the Loose Cannon", 
         "level":30, 
         "id":222 
         } 
        } 
       ]{ 
        "level":30, 
        "name":{ 
         "data":"MEepMorp" 
        } 
       } 

1)的實施例Ⅰ可以NG-重複通過該陣列的匹配項鍵?

在這個例子中,ng-repeat =「數據匹配」返回一個包含兩個項目的數組,這兩個項目都是數組。所以一個多維數組。

回答

2

當然,ng-repeat =「在數據中匹配」將會達到您的對象的第一個級別,那麼您必須做另一個才能到達第二個級別。

問題:你錯過了一個逗號嗎?

]{ 
    "level":30, 
    "name":{ 
     "data":"MEepMorp" 
    } 
} 
0

我沒有正確理解ng-repeat是如何工作的。

一旦我改成了.. 匹配模式:{{item.mode}}

它工作得很好。