javascript - pass array from database into controller AngularJs -


i have little problem app i'm building learn angularjs.

it football stats app. choose home team , away team 2 dropdown menus. have math operations , show results.

this html code:

               <div class='dropdown'>          <span>seleziona nazione:      </span>   <select class='opzioni' ng-model="nazioniselected">   <option ng-repeat="nazione in nazioni track $index" value="{{nazione}}">{{nazione}}   </option>   </select>   </div>    <div class='dropdown2'>   <span>seleziona campionato:      </span>   <select class='opzioni' ng-model="campionatoselected">   <option ng-repeat="team in teams  | filter: {paese:nazioniselected} track $index" value="{{team.campionato}}">{{team.campionato}}   </option>   </select>   </div>     <div class='squadracasa'>   <span>seleziona squadra casa: </span>     <select class='opzioni' ng-model="hometeamselected" >         <option ng-repeat="team in teams  | filter:    {campionato:campionatoselected, paese:nazioniselected} track $index"     value='{{team.nome}}'>         {{team.nome}}        </option>     </select>     </div>      <div class='squadratrasferta'>    <span>seleziona squadra trasferta:      </span>     <select class='opzioni' ng-model="awayteamselected">        <option ng-repeat="team in teams | filter:    {campionato:campionatoselected, paese:nazioniselected} track $index"      value='{{team.nome}}'>{{team.nome}}        </option>     </select>     </div>        <div class='infocasa'>          <ul ng-repeat='team in teams | filter:  {nome: hometeamselected}  track $index'>         <img  ng-show='hometeamselected' src="{{team.stemma}} ">      <p class='nome' ng-show='hometeamselected'> {{team.nome}}  </p>      </ul>        </div>          <div class='infotrasferta'>       <ul ng-repeat='team in teams | filter:  {nome: awayteamselected}  track $index'>         <img  ng-show='awayteamselected' src="{{team.stemma}} ">        <p class='nome2' ng-show='awayteamselected'> {{team.nome}}   </p>            </ul>           </div>         <div class="calcolo"   ng-show='awayteamselected'>       <p>      doppia chance             {{doppia}}      </p><br><br>       <p>       1x2      </p><br><br>       <p>      on 1,5      </p><br><br>       <p>      on 2,5      </p>         <button class="calcola"  ng-click='calcolarisultato(hometeamselected,awayteamselected)' > calcola       </div> 

my problem is: in ng-click want pass not name, team variable, because need data teams selected.

for controller , not working:

              footballnumbers.controller('teamcontroller', function($scope, $route, $routeparams, $http) {      $http.get('/api/teams').then(function(response) {          $scope.teams = response.data;          console.log(response.data);      });      var squadra = $scope.teams;      $scope.nazioni = ['austria', 'belgio', 'bulgaria', 'croazia', 'danimarca', 'finlandia',          'francia', 'germania', 'grecia', 'inghilterra', 'italia', 'norvegia', 'olanda',          'polonia', 'portogallo', 'rep. ceca', 'romania', 'russia', 'spagna', 'turchia', 'svezia',          'svizzera', 'ucraina'      ];      $scope.calcolarisultato = function(squadra1, squadra2) {          (i = 0; < squadra.length; i++) {              (j = 0; j < squadra.length; i++) {                  if (squadra[i].nome == squadra1) {                      if (squadra[j].nome == squadra2) {                          var media1 = (squadra[i].classifica + squadra[i].classificacasa +                              squadra[i].forma) / 3;                          var media2 = (squadra[j].classifica + squadra[j].classificatrasferta +                              squadra[i].forma) / 3;                          if ((media1 + 3) <= media2) {                              $scope.doppia = '1 x';                          } else if ((media2 + 3) <= media1) {                              $scope.doppia = 'x 2';                          } else {                              $scope.doppia = 'niente';                          }                      }                  }              }          }      }  }); 

it says me squadra not defined.

this solution but, said, pass in function hometeam , awayteam data, not name did in code. please?

you init teams async function init squadra variable before these values. instead of using squadra value inside controller, use $scope.teams , remove line:

var squadra = $scope.teams; 

then wherever in controller use squadra, change $scope.teams

also think define constants separated angularjs constants instead of listing them in controller, , in controller inject constant. keeps code cleaner , more reusable.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -