2016-09-23 66 views
0

我有我使用CartoDB的代碼。假設使用JS庫運行查詢,然後返回一些數據。我加了一些結果,它在done()函數內工作。雖然第二個我嘗試在AngularJS中使用/設置結果作爲範圍變量,但我失去了它。這是一些示例代碼。 編輯:我的道歉,「不工作」意味着我總是得到mvcTotal = 0的默認值,而不是在JS內部計算。AngularJS範圍變量沒有在promise中設置

angular.module('angMvcApp') 
    .controller('DataCtrl', ['$scope', '$routeParams', function ($scope, $routeParams) { 
    var borough = $routeParams.borough; 

    var sql = new cartodb.SQL({user: 'wkaravites'}); 
    var table = 'qiz3_axqb'; 
    var mvcTotal = 0; 

    if (borough != null) { 
     $scope.borough = borough; 
    } else { 
     $scope.borough = "Data Overview"; 

     sql.execute("select borough, count(*) from qiz3_axqb group by borough") 
     .done(function (data) { 
      $.each(data.rows, function (index, value) { 
      console.log("pizza: " +value['count']); 
      mvcTotal += value['count']; 
      //$('#' + value['borough'] + '').text(value['count']); 
      //$('#boroughList').append('<h2>' + value['borough'] + '</h2>'); 

      }); 
      //I see this correct 
      console.log(mvcTotal +" totals"); 

      //This doesn't work 
      $scope.mvcTotal = mvcTotal; 

     }) 
     .error(function (errors) { 
      // errors contains a list of errors 
      console.log("errors:" + errors); 
     }); 
     console.log(mvcTotal+" test test"); 

     //This doesn't work 
     $scope.mvcTotal = mvcTotal; 

     //This works 
     #scope.mvcTotal = 57; 

    } 


    }]); 

我搞砸了我是如何將常規變量翻譯成角度範圍變量?當我檢查JS控制檯時,我在裏面看到帶有比薩餅的日誌,並在最後附加了「總計」的正確數字。

下面是HTML視圖:你想從你的數據庫調用返回的數據做

<div class="container"> 
    <div class="row"> 
    <div class="col-lg-12"> 
     <h1>{{borough}}</h1> 
     <p>{{mvcTotal}} motor vehicle collisions</p> 
    </div> 
    </div> 
</div> 
+0

嘗試在運行查詢之前在頂部定義$ scope.mvcTotal。例如$ scope.mvcTotal; var sql = new cartodb.SQL({user:'wkaravites'}); var table ='qiz3_axqb'; var mvcTotal = 0; – user2085143

+0

「不起作用」太含糊。我們不知道你的程序的預期行爲是什麼。你的意思是它沒有出現在HTML視圖中?你應該包含html視圖。只是console.log消息是錯誤的嗎? –

回答

0

事情,都必須內完成的功能來完成。我想你明白這一點,但你缺少的是操作的順序。使用調試器查看代碼的實際執行順序:

var sql = new cartodb.SQL({user: 'wkaravites'}); 

//step #1 
var mvcTotal = 0; 

    //step #2 
    sql.execute("select borough, count(*) from qiz3_axqb group by borough") 
    .done(function (data) { 
     //step #6 finally after DB responds this is called. mvcTotal is still 0 
     //$scope.mvcTotal is 57 
     $.each(data.rows, function (index, value) { 
     console.log("pizza: " +value['count']); 
     mvcTotal += value['count']; 


     }); 
     //step #7 now you get the correct total 
     console.log(mvcTotal +" totals"); 

     //step #8 this is correct. you can display this value in your html view as {{mvcTotal}} 
     $scope.mvcTotal = mvcTotal; 

    }) 


    //step #3 mvcTotal is still 0 
    console.log(mvcTotal+" test test"); 


    //step #4 mvcTotal is still 0 since DB call has not finished 
    $scope.mvcTotal = mvcTotal; 

    //step #5 previous value was 0, now it's 57 
    $scope.mvcTotal = 57; 
+0

我試過了,只是把它放在DONE函數中,我仍然得到0作爲mvcTotal的HTML值。另外,更好的解釋。 – Zeratas

+0

奇怪,它隨機工作,然後有時不隨機。 – Zeratas

+0

我會完全刪除'sql.execute'語句下面的代碼(步驟3-5)。這隻會讓事情混淆。 –