Angular-styleguide: Arrays under scope.vm of directive undefined

Created on 13 Jun 2017  ·  7Comments  ·  Source: johnpapa/angular-styleguide

Greetings,

When following Angular 1.x style guide for directives, if I am referencing the array scope.vm.virtStats from the controller, it comes back as undefined. but if I reference simple object from the controller such as scope.vm.title, it does return a value. Why is that? Because I do see scope.vm as having that virtStats
array in the console.

        .module('app.dashboard')
        .controller('Dashboard', Dashboard)
        .directive('d3Chart', buildChart);
       `function buildChart() {
            var directive = {
                restrict: 'EA',
                link: linkFunc,
            };

            return directive;

            function linkFunc(scope, el, attr, ctrl) {
            ...lots of d3 visualization code here
                console.log(scope.vm)
            }


function Dashboard($state, $scope, dataservice, logger) {
        var vm = this;
        vm.virtStats = [];

        vm.title = 'Dashboard';

        clearSearchResult();

        activateVirtsData();

        function clearSearchResult() {
             vm.virtStats = [];
        };


        function activateVirtsData() {
            return dataservice.getVirtStatsData().then(function (data) {
                vm.virtStats = data;
                return vm.virtStats;
            });     
        } );            
        }; 

All 7 comments

@yosiasz This happens because you are trying to access the array inside the linkfunction of the directive which is called before the activateVirtsData

thanks @bampakoa!!! junk! been staring at this code too long.

@bampakoa question. but controller is being called before .directive?

.controller('Dashboard', Dashboard)
 .directive('d3Chart', buildChart);

@yosiasz They are not executed in the order that you declare it. Even if you do this:

 .directive('d3Chart', buildChart)
.controller('Dashboard', Dashboard)

the runtime order will be the same. Let me clarify if you did not get it.

Very informative! So I will need to figure out how to populate that array so that linkFunc has access to that array. thanks!

Is this tribal knowledge or is there documentation for this behavior?

I think you can find related information on the AngularJS official documentation.

Was this page helpful?
0 / 5 - 0 ratings