我正在尝试编写我的第一个AngularJS指令;它基本上是一段代码,可以在最大星号数量内呈现出一定数量的星号(徽章)。
指令中的链接:函数似乎无法访问传入的作用域变量,除非它们是静态值(尽管模板代码可以)。
我需要把链接:代码放在其他地方吗?
HTML代码:
<span class="badge"><span sa-motes mote-count="character.attributes.stamina.value" mote-max="5" stat-name="character.attributes.stamina.name"></span> </span>
mote-max="5"可以实现,但mote-count="character.attributes.stamina.value"无法实现(在作用域中显示为空)。
该局部控制器已加载角色数据。
指令:
sistemaDirectives.directive('saMotes', function() {
return {
restrict: 'A',
scope: {
moteCount: '=',
moteMax: '=',
statName: '@'
},
template: '<span ng-repeat="mote in motes" class="glyphicon glyphicon-certificate" ng-class="{\'mote-empty\':$index>moteCount+1}"> </span>',
link: function(scope, elem, attrs, ctrl ) {
scope.motes = [];
for (var i = 0; i< scope.moteMax; i++) {
scope.motes.push( i );
}}}});
我在我的视图中尝试了这个内联代码,它有效:
<span class="badge">
<span class="glyphicon glyphicon-certificate" ng-repeat="i in getNumber(character.attributes.strength.value) track by $index"></span><span class="glyphicon glyphicon-certificate mote-empty" ng-repeat="i in getNumber(character.attributes.strength.max - character.attributes.strength.value) track by $index">
</span></span>
getNum函数只是在控制器中创建一个数组:
$scope.getNumber = function(num) {
return new Array(num);
}
Help?
link
下是否有意将参数命名为scope
而不是$scope
? - Nick McCurdy