簡體   English   中英

使用服務訪問angularjs控制器的Json鍵值對

[英]accessing Json key-value pair for angularjs controller using services

我一直在嘗試將我的服務中的數據用於我的angularjs控制器中的圖表。我已經在我正在控制器中使用的service.js中提供了一項服務。我得到的只是對象數組,但是當我嘗試訪問值時在對象數組中,它會為我使用的每種方法引發錯誤。下面是我在做什么

 My Service 

  var demoService= angular.module('demoService',[])
 .service('myService',function($http,$q){
  var deferred = $q.defer();
  $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data)
        {
          deferred.resolve(data);
        });
this.getPlayers=function()
{
    return deferred.promise;
}
})

我的控制器

 angular.module("app.controllers", [])
.controller("gaugeCtrl", ["$scope","config","myService",
function ($scope,config,myService) {
var promise=myService.getPlayers();
promise.then(function(data)
        {
    $scope.players=data.data;
    //console.log($scope.players.Tweets);
     ($scope.players.Tweets).each(function(index, element) {
        console.log(element.FAVOURITE_COUNT); 
     });
    });
    return $scope.gaugeHome = {
      gaugeData: {
        maxValue: 9000,
        animationSpeed: 100,
        val: 1000


      },
      gaugeOptions: {
        lines: 12,
        angle: 0,
        lineWidth: 0.47,
        pointer: {
          length: 0.6,
          strokeWidth: 0.03,
          color: "#555555"
        },
        limitMax: "false",
        colorStart: config.secondary_color,
        colorStop: config.secondary_color,
        strokeColor: "#F5F5F5",
        generateGradient: !0,
        percentColors: [
          [0, config.secondary_color],
          [1, config.secondary_color]
        ]
      }
    }

我還在我的app.js中包含了服務模塊:

   var app = angular.module("app",["demoService"])

我如何正確地為我的gaugectrl訪問特定值。是否以錯誤的方式訪問它?

 ($scope.players.Tweets).each(function(index, element) {
        console.log(element.FAVOURITE_COUNT); 

提前致謝

看起來一切正常。 似乎您正在嘗試錯誤地訪問數組中的對象。

我認為您想使用Array.forEach

someArray.forEach(function(element, index, array) {
   // Do stuff here.
});

我對您的代碼做了簡化:

 var app = angular.module('app', []); app.controller('myController', function($scope, $q, $http) { var deferred = $q.defer(); $http.get('http://enlytica.com/RSLivee/rest/census').then(function(data) { deferred.resolve(data); }); var promise = deferred.promise; promise.then(function(data) { var tweets = []; var total = 0; $scope.players = data.data; ($scope.players.Tweets).forEach(function(element) { console.log(element); tweets.push({FAVOURITE_COUNT: element. FAVOURITE_COUNT, TWEET_ID: element.TWEET_ID}); total += element.FAVOURITE_COUNT; }); $scope.something.tweets = JSON.stringify(tweets, null, 2); $scope.something.favoriteTotal = total; }); $scope.something = {}; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <div ng-app='app' ng-controller='myController'> {{ something.favoriteTotal }} <pre>{{ something.tweets }}</pre> </div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM