Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays under scope.vm of directive undefined #840

Closed
yosiasz opened this issue Jun 13, 2017 · 7 comments
Closed

Arrays under scope.vm of directive undefined #840

yosiasz opened this issue Jun 13, 2017 · 7 comments

Comments

@yosiasz
Copy link

yosiasz commented Jun 13, 2017

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.

angular
        .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;
            });     
        } );            
        }; 
@bampakoa
Copy link

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

@yosiasz
Copy link
Author

yosiasz commented Jun 13, 2017

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

@yosiasz
Copy link
Author

yosiasz commented Jun 13, 2017

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

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

@bampakoa
Copy link

@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.

@yosiasz
Copy link
Author

yosiasz commented Jun 13, 2017

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

@yosiasz
Copy link
Author

yosiasz commented Jun 13, 2017

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

@bampakoa
Copy link

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

@yosiasz yosiasz closed this as completed Jun 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants