2

I am stuck in here. I went through few tutorials and Stack overflow questions and answers like here.

But I can't resolve my problem. Basically what I want to do here is just read the JSON file from my local and pass it into my custom directive scope.

my html

<div ng-app="myApp" ng-controller="Ctrl" >
    <bars-chart chart-data="myData"></bars-chart>
</div>

script

var app = angular.module('myApp',[]);
app.controller('Ctrl', function($scope, $http){
    $http.get('practice.json')
    .then(function(resource){
    $scope.myData = resource.data;               
    });
}); 
//directive 
app.directive('barsChart', function(){
var rectBar = {
    restrict : 'EA',
    replace : true,
    scope : {data:'=chartData'},
    link : function(scope, element, attr){
        var barColor = 'steelblue';

//doing some other stuff

        }
    };
    return rectBar;
});

when I debug in directive scope I can't get the scope.data that's why I get error when I try to do some stuff wit scope.data. But its works fine when I hard coded in my controller like

app.controller('Ctrl', function($scope,) {
  $scope.myData = [
                    {"State":"AL","freq":{"low":4786, "mid":1319, "high":249}}
                    ,{"State":"AZ","freq":{"low":4786, "mid":1319, "high":418}}
                    ];
});

note: I run my index.html file into python server by creating from command line python -m http.server

1 Answer 1

2

But its works fine when I hard coded in my controller

Your code looks ok except you get $scope.myData by async way and your directive doesn't know about.

You can define watcher inside directive that will listen on data change

Something like:

 var cancelWatch = scope.$watch(function () {
        return scope.data;
    },
    function (newVal, oldVal) {
        if (newVal !== undefined ) {
            run();
            cancelWatch();  // in case if you want kill watcher
            }
    });

Demo


Full directive:

app.directive('barsChart', function(){
var rectBar = {
    restrict : 'EA',
    replace : true,
    scope : {data:'=chartData'},
    link : function(scope, element, attr){
        var barColor = 'steelblue';

     function run(){
       console.log(scope.data);
     }

     var cancelWatch = scope.$watch(function () {
        return scope.data;
    },
    function (newVal, oldVal) {
        if (newVal !== undefined ) {
            run();
            cancelWatch();
            }
    });


        }
    };
    return rectBar;
});
Sign up to request clarification or add additional context in comments.

3 Comments

@downvoter please describe what is wrong with my answer
thanks @Maxim Shoustin. Will you little bit explain how cancelWatch() work? and $scope.data set to newVal. Here I noticed when I call cancelWatch(), $scope,data may undefined as you describe your answer async way data get.
@R.A.Munna multiple watchers is expensive item and if you have 2000 watchres - it can slow down your site. Sometimes if you want to update your directive once only - you can kill watcher. $scope.$watcher returns cancel callback. So once we got scope.data we kill it by calling cancelWatch() method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.