2014-10-30 85 views
1

指令中score_stats的值始终未定义,并且在父范围中,stats变量具有正确的值。我究竟做错了什么?使用ng-repeat绑定分离范围的角度指令

我在模板这个代码(和fixtures_stats是一个数组):

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

该代码在指令:

{                  
    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); 
    },                    
}; 

该代码在指令中的模板:

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

在控制台中,我得到:

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

回答

1

首先,你应该使用一个表达式score_stats属性:

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

然后在指令的定义,而不是scope_stats scope属性将成为camelCase'ed:

scope: { 
    scoreStats: '=' 
} 

所以指令模板您需要更改为:

Scenarios for {{ scoreStats.score1 }}-{{ scoreStats.score2 }} score combo 
+0

无变化: score_stats在指令 – user1387717 2014-10-30 18:46:30

+0

中仍未定义。对范围属性名称中的_ _分隔符存在一些混淆。我会建议使用常见的蛇情况来预测结果。查看更新的答案。 – dfsq 2014-10-30 18:57:59

+0

啊,工作!非常感谢你! – user1387717 2014-10-30 19:04:20