2015-09-09 60 views
0

首先對於壞的英語感到抱歉。我有一個關於angularjs的問題。有了這個http post請求,我從mysql json格式獲得了相同的數據。AngularJS在JSON之後等待觀看

app.controller('DeviceActivityCtrl', function ($scope, $rootScope, $location, $http, Data,$stateParams,DeviceActivityResult) { 

    //$scope.foodDistribution=null; 

    if (DeviceActivityResult.status == "success") { 
     $scope.deviceActivityLog = DeviceActivityResult; 
     $scope.authError = 'Correct'; 
     alert(DeviceActivityResult.data[0].steps); 
    } 
    else { 
     $scope.authError = 'notsuccess'; 
    } 


}); 

問題在這裏。當視圖(html)被加載時,如果http請求沒有完成。無法加載HTML。因爲百分比:數據(未準備好)。我如何等待json的加載視圖(html)完成它的工作。感謝您的幫助

<div class="inline"> 
       <div ui-jq="easyPieChart" ui-options="{ 
        percent: ({{deviceActivityLog.data[0].activity_time}}/340)*100, 
        lineWidth: 10, 
        trackColor: '{{app.color.light}}', 
        barColor: '{{app.color.success}}', 
        scaleColor: '{{app.color.light}}', 
        size: 188, 
        lineCap: 'butt' 
       }"> 
        <div> 
         <span class="h2 m-l-sm step">{{(deviceActivityLog.data[0].activity_time/340)*100|number:0}}</span>% 
         <div class="text text-sm"></div> 
        </div> 
       </div> 
      </div> 

----------------狀態---------------

.state('report.food', { 
       url: '/{userid:[0-9]{1,4}}', 
       templateUrl: 'tpl/report_food.html', 
       controller: 'DeviceActivityCtrl', 
       resolve: { 
        deps: ['$ocLazyLoad', 
         function ($ocLazyLoad) { 
          return $ocLazyLoad.load('ui.grid').then(
           function() { 
            return $ocLazyLoad.load('js/app/report/reportFood.js','UiGridDemoCtrl.resolve'); 
           } 
          ); 
         }, 
        ], 
        DeviceActivityResult: function(Data, $stateParams) { 
         return Data.post('deviceActivity', 
          {userid: $stateParams.userid} 
         ); 
        } 
       } 
      }) 

回答

0

它看起來你正在使用ui-router,所以你可以在轉換之前'解析'狀態數據。這裏是你如何做到這一點的例子:

.state('yourState', { 
    ... 
    controller: 'DeviceActivityCtrl', 
    resolve: { 
     DeviceActivityResult: function(Data, $stateParams) { 
      return Data.post('deviceActivity', 
       {userid:$stateParams.userid} 
      ); 
     } 
    } 
}) 


.controller('DeviceActivityCtrl', function ($scope, $rootScope, $location, $http, DeviceActivityResult) { 
    // Data.toast(DeviceActivityResult); 
    if (DeviceActivityResult.status == "success") { 
     $scope.deviceActivityLog = DeviceActivityResult; 
     $scope.authError = 'Correct'; 
    } 
    else { 
     $scope.authError = 'notsuccess'; 
    } 
}); 
+0

謝謝你的回答。我添加了我的狀態表單。在這個JS我有4個控制器。 @gpicazo – erdinc

+0

錯誤:[$ injector:unpr]未知提供者:DeviceActivityResultProvider < - DeviceActivityResult < - DeviceActivityCtrl。我試試你的答案,給出這個錯誤。你有任何解決方案? @gpicazo – erdinc

+0

您應該可以將解析對象注入控制器。檢查你拼寫正確。 – GPicazo