1

I can currently display the desired values in text in the DOM:

<li id="dates" class="dates" ng-repeat="(key,value) in players">
       {{value.Session_Date}}
    </li>

I need each of the values that is repeated to be accessible, so that each can be pushed to an array. I have tried many different things like different variations on array.push(angular.element('#dates').scope());

and nothing has worked. Is the problem the data itself? $scope.players is assigned to a returned array of documents from mongoDB, queried using the following server side code:

collection.find().toArray(function(er,docs){  
        if(err) { return console.dir(err); }
        res.send(docs);

the 'Session_Date' is an inner JSON value of a particular document. These inner values need to be accessible.

EDIT: I'll be clearer:

The query is accessed with an AJAX call in the service:

app.factory('dates', ['$resource', function($resource) {
  return $resource('/orders/api/dates', {}, {
    get: {
      isArray: true
    }
  });
}]);

Which is called in the controller:

app.controller('PlayersController', ['$scope', 'api', '$rootScope', function($scope, api, $rootScope) {
  api.get(function(data) {
    $scope.players = data;
  });

}]);
Jack
  • 23
  • 6
  • You want access to the **li** elements or the values shown? You already have all the data in the _players_ array, why push them into a new array? And if your trying to push li elements into an array then your doing something wrong anyway. You shouldn't access elements and manipulate them when using angular – Robert Kullamaa Jun 28 '15 at 12:27
  • The elements shown, so for example, one value of {{value.Session_Date}} might be '17July15' and I want to pass each of these strings that are currently displayed to an array. – Jack Jun 28 '15 at 12:29
  • your question isn't exactly clear. `ng-repeat` is just a display iterator. It doesn't *do* anything with your data but display it. If `ng-repeat` can display your data, you *already* have access to it. and trying to access the data via `#dates` is not "angular", it's more like the way you might work in jquery. – Claies Jun 28 '15 at 12:31
  • so to help you clarify your issue, my question to you is, **when** do you want to push your elements into an array? When it's being loaded from the server? When it is first being displayed on the HTML? When the user pushes a button? or something else? – Claies Jun 28 '15 at 12:33
  • Ideally I want the array to be populated first (after query on server side has been made) and then rendered in html (in highcharts, to be clear) – Jack Jun 28 '15 at 12:35
  • also, `(key, value) in players` isn't exactly intuitive; are you getting an *array* of players back, or an *object*? – Claies Jun 28 '15 at 12:36
  • Players is an array of documents from mongodb. Thanks for helping me with this. Have been trying to do this for days – Jack Jun 28 '15 at 12:38
  • I think you need to reconsider your question here. It seems like you aren't trying to figure out how to access a specific property, you are trying to figure out how to correctly reference the data you are getting back from you server request. It would be easier to help you if you could show the server call you are making, and the structure of the data you are getting back. – Claies Jun 28 '15 at 12:38
  • normally trying to hold a conversation through comments is discouraged here, but you don't have enough rep to use the chat services. however, comments are all but useless for code snippets... please edit the question with any new code. – Claies Jun 28 '15 at 12:41
  • ok, so it doesn't look like you are doing anything extraordinary here; your `$scope.players` should be an array of players, so instead of `(key,value) in players` (which is for iterating through object properties), you should be able to use `player in players` and then use `player.Session_Date`. – Claies Jun 28 '15 at 12:45
  • and in the JavaScript, you can reference `players[0].Session_Date`. – Claies Jun 28 '15 at 12:50
  • I've tried that already and I get 'cannot find property '0' of undefined. – Jack Jun 28 '15 at 12:58
  • well, to be more precise, in the JavaScript, you would have to reference `$scope.players[0].Session_Date`. – Claies Jun 28 '15 at 12:59
  • Still getting 'Cannot read property '0' of undefined' – Jack Jun 28 '15 at 13:02
  • what is in `$scope.players` if you log it to the console? – Claies Jun 28 '15 at 13:03
  • [e, e, $promise: Promise, $resolved: true]0: e$$hashKey: "object:15"Drill End Time: "20:27:59"Drill Start Time: "18:55:25"Dynamic Stress Load: "246"Player_Display_Name: "Ronaldo"Session_Date: "18Jul2014"_id: "5589ab481fe3961d7936cb4d"__proto__: e1: e$promise: Promise$resolved: truelength: 2__proto__: Array[0] – Jack Jun 28 '15 at 13:24
  • looks like your `api` is returning a promise, not the actual array. – Claies Jun 28 '15 at 13:44
  • The api now returns an array (Array[2]0: Object$$hashKey: "object:15"Drill End Time: "20:27:59"Drill Start Time: "18:55:25"...) and when I run console.log($scope.players[0].Session_Date) the data is correctly output to the console. If I try to do anything else with the data however, like place it in a separate array, it treats it as undefined?! – Jack Jun 28 '15 at 16:05
  • Fixed it now. Thank you very much for your help and attention! :) – Jack Jun 28 '15 at 19:51

0 Answers0