2013-12-19 30 views
1

我遇到的問題會使用從AngularJS(v1.2.5)服務調用填充的數據呈現Flotcharts。如果數據是硬編碼的,則圖表按預期呈現,但在從$ resource調用中分配值(控制檯日誌記錄顯示「未定義」作爲值)時不會顯示。Flotcharts未使用angularjs進行呈現

奇怪的是,我用它來填充圖表數據相同的變量在視圖中顯示,並能正常工作,這使得我認爲這可能是某種形式的競爭條件或範圍界定問題。

我曾嘗試分配的$範圍變量作爲解決的承諾沒有成功的結果,糾正比賽條件,以及嘗試的東西,如$範圍。$適用()從圖表指令。

下面是我的應用程序的代碼摘錄。任何幫助表示讚賞。謝謝。

HTML包裝器:

<div class="chart-title">{{getCount.count}} Total Transactions</div> 
<div id="deposits" data-chart="pie" data-flot-chart data-ng-model="model" data-ng-controller="OverviewCtrl"></div> 

主模塊( 「應用程序」)::從概述路線

<!DOCTYPE html> 
<html data-ng-app="app" data-ng-controller="app"> 
    <head> 
     <title>FlotChart Example</title> 
    </head> 

    <body> 
     <main data-ng-view></main> 
    </body> 
</html> 

模板

;(function (angular) { 
    'use strict'; 

    angular.module('app', ['ngRoute', 'ngResource', 'ngTouch', 'services.Localization', 'Login', 'Overview']) 
     .config(['$routeProvider', function ($routeProvider) { 
      $routeProvider 
      .when('/', { 
       templateUrl: 'views/login.html', 
       controller: 'LoginCtrl' 
      }) 
      .when('/overview', { 
       templateUrl: 'views/overview.html', 
       controller: 'OverviewCtrl' 
      }) 
      .otherwise({ 
       redirectTo: '/' 
      }); 
     }]) 

     .controller('App', ['$rootScope', 'Localize', function ($rootScope, Localize) { 
      // initialize localization 
      $rootScope.labels = Localize.get(); 
     }]); 
}(angular)); 

概述控制器(在圖和視圖中使用) :

;(function (angular) { 
    'use strict'; 

    angular.module('Overview', ['services.DepositsSvc']) 

     .controller('OverviewCtrl', ['$rootScope', '$scope', 'DepositCount', 'DepositCountGET', function ($rootScope, $scope, DepositCount, DepositCountGET) { 

      $scope.getCount = DepositCount.get(); 

      $scope.getGETCount = DepositCountGET.get(); 

      $scope.model = {}; 

      $scope.model.data = [ 
       { 
        label: $rootScope.labels.gets, 
        data: $scope.getGETCount.count, 
        color: "#e4d672" 
       }, 
       { 
        label: $rootScope.labels.puts, 
        data: $scope.getCount.count, 
        color: "#c2cfeb" 
       } 
      ]; 

      $scope.model.options = { 
       series: { 
        pie: { 
         show: true, 
         radius: 1, 
         label: { 
          radius: 2/3, 
          formatter: function (label, series) { 
           return '<div class="pie">' + label + ': ' + 
            series.data[0][1] + '<br>(' + Math.round(series.percent) + '%)</div>'; 
          } 
         } 
        } 
       }, 
       legend: { 
        show: false 
       } 
      }; 
     }]); 
}(angular)); 

服務(與兩個電話輸出爲{數:數}):

;(function (angular) { 
    'use strict'; 

    angular.module('services.DepositsSvc', ['ngResource', 'ngRoute']) 

     .factory('DepositCount', ['$resource', function ($resource) { 
      return $resource('/rest/deposits/count', {}, { 
       query: { method: 'GET', params: {}, isArray: true } 
      }); 
     }]) 

     .factory('DepositCountGET', ['$resource', function ($resource) { 
      return $resource('/rest/deposits/countgetdeposits', {}, { 
       query: { method: 'GET', params: {}, isArray: true } 
      }); 
     }]); 
}(angular)); 

圖表指令:

;(function (angular, $) { 
    'use strict'; 

    angular.module('directives.FlotCharts', []) 

     .directive('flotChart', function() { 
      return { 
       restrict: 'EA', 
       controller: ['$scope', '$attrs', function ($scope, $attrs) { 
        var plotid = '#' + $attrs.id, 
         model = $scope[$attrs.ngModel]; 

        $scope.$watch('model', function (x) { 
         $.plot(plotid, x.data, x.options); 
        }); 
       }] 
      }; 
     }); 
}(angular, jQuery)); 

回答

0

你應該嘗試提煉下來,以適合在一個較小的例子jsFiddle,所以我們可以試試看。

有一兩件事,我注意到,雖然是,你正在做一個淺淺的手錶,然後設置model.data和model.options。所以,除非我錯過了一些東西,否則當這些變化時手錶不會閃光。只有模型本身發生變化時。嘗試通過true作爲第三個參數來觀看。

+0

謝謝你,我會在工作的jsfiddle此問題。添加TRUE;作爲第三選項並沒有在我現有的實施工作。 – Paul

0

我不知道這將解決您的問題(因爲這也許許多問題的結果),但我覺得你得到了指令範圍和模式附件錯誤:

*。我不確定你爲什麼要將指令封裝爲自調用函數(從來沒有看到過這種風格,並且肯定不需要)。

*。您正在使用('directives.flotCharts',[])語法創建新模塊,但沒有[]你可以在指令連接到任何現有的模塊。的重要組成部分的是,這個模塊未注入的應用程序!你應該包括它的app模塊依賴數組中。如果沒有它,棱角分明不知道這個指令(並且還包括index.html中的js文件...我有時會忘記並且想知道爲什麼這不起作用) *。我建議將指令改寫爲(並注意這些註釋): 這樣的事情:

angular.module('directives.FlotCharts', []) // creaating a new module here 
    .directive('flotChart', function() { 
     return {o 
      restrict: 'EA', 
      scope:{model:"=ngModel"},//creating a two ways binding to ngModel 
      controller: ['$scope', '$attrs','$element', function ($scope, $attrs,$element) { 
       var plotid = '#' + $attrs.id, // you are doing an ugly hack instead of using $element, which comes with the directive, $element is a jquery (or jquery lite) object (if you included jQuery **before** angular.js in index.html it is a jQuery object) 


        $scope.$watch(function(){return $scope.model}, function (x) { 
         if (!x){ 
          return; 
         } 
         $.plot(plotid, x.data, x.options);//may need to wrap this in an $apply, depends.. //surly this should be called on the $element object - something like: $element.plot(x.data, x.options) although I don't now the specifics of this lib. 
        }, true); //the true is needed for a deep equality and not just shallow check, which sometimes has problems with objects.. not sure you need this here. 
       }] 
      }; 
     }); 

我還懷疑你是錯的治療承諾。但不知道這是這裏的問題(雖然當其他事情都起作用時,這很可能會成爲問題)。爲了回答這個問題並提出問題,我認爲你應該將承諾問題細化到一個不同的具體問題上

好運氣!

+0

謝謝你的我正在爲這個問題工作jsFiddle,因爲它不是反饋的工作 關於問題/評論: *使用IIFE,因爲嚴格模式並傳入angularjs和jquery對象 *創建爲獨立模塊以注入其他幾個模塊;希望這是獨立的 *重寫指令利用$元素,而不是jquery hack。 – Paul

+0

[jsFiddle](http://jsfiddle.net/paulmc/H6Y5c/24/)添加。我已經精簡了代碼,甚至超過了本主題中介紹的代碼。發現設置圖表的硬編碼值不在小提琴中工作,而是在本地進行。我加載了除CDN以外的所有JS依賴項(粘貼在小提琴中)和Flot Charts代碼(來自原始github)。 – Paul

+1

@paul先 - 讓我們修復小提琴。如果你打開控制檯,你會在整個地方看到錯誤消息,至少前兩個可以修正爲** [描述](http://geekdave.com/2013/06/19/linking-raw- github-files-from-jsfiddle /)這裏**。 - 解決github.raw問題,打破了flot圖 – alonisser