AngularJS scope values undefined in link -


i'm trying figure out scope , link when directive initialized. have directive in tree controller display details @ branch point:

<typelists sub="branch.subbranches[0]"></typelists> 

the (relevant parts of the) directive handles branch info below:

listsapp.directive(     'typelists',     function($rootscope) {         return {             restrict: 'ea',             replace: true,             scope: {                 branch : '=sub'             },             templateurl: templatedir + '/typelists.html',             link: function (scope, element, attrs) {                 console.log(scope,scope.branch); // debug                  // other stuff                 //  (including working reload() , refresh() methods)                  scope.subject = 'type' + scope.branch.model.getfilter() + 'lists';                  // catch $rootscope.$broadcasts branch                 $rootscope.$on( scope.subject+'refresh', function() { scope.refresh(); ) } );                 $rootscope.$on( scope.subject+'reload', function() { scope.reload(); } );             }         }; 

now, confusing bajeezus out of me in // debug line, can see .branch populated expected in output of scope alone, scope.branch shows undefined. means when try set scope.subject down below, instead of getting typeid parent type, i'm getting 'undefined' instead of getting unique branch tag such 'type1lists' i'm getting 'typeundefinedlists', $on watch functions aren't triggering properly.

why unable access scope.branch or why showing undefined when try? (especially when can see in same console.log output part of scope?)

thanks in advance help.

how branch.subbranches[0] populated? bet value set after link function of directive runs.

you can either make directive resilient these changes, so:

var unwatch = scope.$watch("scope.branch", function(v){   if (v) {     unwatch(); // removes watch when value not undefined     init(); // run init code   } }); 

or, instantiate directive when data ready:

<typelists ng-if="branch.subbranches[0] !== undefined"             sub="branch.subbranches[0]"></typelists> 

p.s.

the reason console.log shows data because (at least in chrome) console "rendering" doesn't happen @ time of logging - in other words, when call console.log data still not there, gets there before console reads rendering purposes.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -