1

The value of score_stats in the directive is always undefined, and in the parent scope the stats variable has the correct value. What am I doing wrong?

I have this code in the template (and fixtures_stats is an array):

div(ng-repeat="stats in fixture_stats")
  scenario-analysis(score_stats="stats")

This code in the directive:

{                                                                    
  templateUrl: '/' + appVersion + '/directives/scenario-analysis.html',                                                                                                                                              
  scope:{
      score_stats:'=score_stats'
  },
  controller: function($scope, $element, $attrs) {
      console.log('directive $scope.score_stats: ' + $scope.score_stats +
                  ', $scope.$parent.stats: ' + $scope.$parent.stats);
  },                                                                               
};

This code in the directive template:

h3 Scenarios for {{ score_stats.score1 }}-{{ score_stats.score2 }} score combo

In the console I get:

directive $scope.score_stats: undefined, $scope.$parent.stats: [object Object]
directive $scope.score_stats: undefined, $scope.$parent.stats: [object Object]

1 Answer 1

1

First of all you should use an expression for score_stats attribute:

div(ng-repeat="stats in fixture_stats")
  scenario-analysis(score_stats="stats")

Then in directive definition instead of scope_stats scope property will become camelCase'ed:

scope: {
    scoreStats: '='
}

so in directive template you need to change to:

Scenarios for {{ scoreStats.score1 }}-{{ scoreStats.score2 }} score combo
Sign up to request clarification or add additional context in comments.

2 Comments

No change: score_stats is still undefined in the directive
There is some confusion with _ separators in scope attribute names. I would recommend to use common snake-case for predictable results. See updated answer.

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.