2

I'm pretty new to this, but I've been working on a basic Angular app and I am running into a small problem. I have a directive which is supposed to receive data from the following block of html:

<div ng-controller="LeagueShowController">
  {{ViewModel.Price}}
  <bars-chart chart-data="ViewModel.price"></bars-chart>
</div>

Where the first instance of "ViewModel.Price" returns the correct array of prices, but the second instance within my directive element (bars-chart) returns an empty array. Here is what my directive looks like (I've removed a little code to simplify it):

"use strict";
(function(){
  angular
  .module("leagues")
  .directive('barsChart', function ($parse) {
    var directiveDefinitionObject = {
      restrict: 'E',
      replace: false,
      scope: {data: '=chartData'},
      link: function (scope, element, attrs) {
        console.log(scope.data);
      }
    };
    return directiveDefinitionObject;
  });
}());

I changed the chart-data="ViewModel.price" to simply chart-data="ViewModel" to see what I get, and here is where my problem occurs. When this is changed, and I console.log scope.data in the directive again, I get an object with and array of zero. However, if I click into that object, I see the information I want. See below for details. Is there a step I'm missing to break down the object further in the directive?

Object {league: k, Price: Array[0]}
   league: k
   Price: Array[4]

EDIT: To clarify, this is not just a console issue. When I try to actually return the data in my directive, it still returns an empty array when I call "ViewModel.Price".

Controller

(function(){
  angular
  .module("leagues")
  .controller("LeagueShowController", [
    "$stateParams",
    LeagueShowControllerFunction
   ]);

  function LeagueShowControllerFunction($stateParams,scope){
    var self = this;
    this.Price = [1,2,3,4];
  }
}());

This is my first post here on SO, so please let me know if I can do a better job posting a question like this. And thank you!

joe-gz
  • 165
  • 1
  • 3
  • 12
  • Possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – JJJ Dec 30 '15 at 19:53
  • I don't think that is the same unfortunately. It's not just a console issue. When I try to actually return the data in my directive, it still returns an empty array when I call "ViewModel.Price". Thanks for taking a look though! – joe-gz Dec 30 '15 at 20:17
  • Unfortunately there isn't enough code here to diagnose the actual problem. It's likely that `chartData` comes from an asynchronous source so that it doesn't exist yet when you call the directive. – JJJ Dec 30 '15 at 20:23
  • There honestly isnt really any other code to provide.. My controller at this point is essentially just a basic array that I'm using to test it out. I'm not sure that it's an asynchronous issue though. That last block that I provide shows that outputs in the console, and the issue is that it returns an array of zero, but then when i actually click into the object there, the array im looking for is inside. does that make sense at all? – joe-gz Dec 31 '15 at 16:57
  • Surely there's *some* code (what does `chartData` contain? Where does it come from?) Please give a *complete* example, or edit this: http://jsfiddle.net/joshdmiller/hb7lu/ until it duplicates your issue. – JJJ Dec 31 '15 at 18:17
  • I added in the controller as well, but it is literally just an array of numbers. Again, when I return "{{ViewModel.Price}}" in the browser, i see the array [1,2,3,4], it's only when I try to pass the same thing to the directive that i get the strange result. when i console.log(scope.data) in the directive, i see this weird empty array in the console and I can't parse down to that Price:Array[4] - Object {league: k, Price: Array[0]} league: k Price: Array[4] – joe-gz Dec 31 '15 at 20:39
  • Sorry, I think you're actually right that it's an asynchronous issue after all. I'm still curious as to why it shows up on the browser without showing in the console, but I'll keep digging. – joe-gz Dec 31 '15 at 20:55

0 Answers0