Angularjs 将$scope.array时的一个值链接到ngRepeat指令模板
我已经将我的代码简化为这个示例,以展示我想要完成的任务 控制器:Angularjs 将$scope.array时的一个值链接到ngRepeat指令模板,angularjs,angularjs-directive,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Ng Repeat,我已经将我的代码简化为这个示例,以展示我想要完成的任务 控制器: angular .module('myApp') .controller('myController', myController); myController.$inject = ['$scope']; function DashboardController($scope) { $scope.someNumbers = [ 'One', 'Two', 'Three', 'Four', 'Five' ]; }
angular
.module('myApp')
.controller('myController', myController);
myController.$inject = ['$scope'];
function DashboardController($scope) {
$scope.someNumbers = [ 'One', 'Two', 'Three', 'Four', 'Five' ];
}
<table class="table table.hover">
<tr class="table-row" ng-repeat="num in someNumbers track by $index" all-numbers="someNumbers" dyn-rows></tr>
</table>
视图:
angular
.module('myApp')
.controller('myController', myController);
myController.$inject = ['$scope'];
function DashboardController($scope) {
$scope.someNumbers = [ 'One', 'Two', 'Three', 'Four', 'Five' ];
}
<table class="table table.hover">
<tr class="table-row" ng-repeat="num in someNumbers track by $index" all-numbers="someNumbers" dyn-rows></tr>
</table>
指令:
angular
.module('myApp')
.directive('dynRows', function() {
return {
restrict: 'A',
template: '<td>{{ allNumbers }}</td',
scope: {
allNumbers: "=",
},
link: function(scope,elem,attrs){
// magic has to happen here?
}
}
}
})
angular
.module('myApp')
.directive('dynRows',function(){
返回{
限制:“A”,
模板:{{allNumbers}}属性all numbers=“someNumbers”
应该是all numbers=“num”
,以便传入索引的值而不是整个数组。很好地回答了我自己的问题,在Daniel的帮助下,我找到了解决问题的最佳方案
正如我所提到的,上面的例子只是我真实代码的一个小例子
在我的实际代码中,我有多个数组(长度相同)。我希望每个数组每次发送一个值
原始代码行
<tr class="dashboard-table-row" ng-repeat="labels in properLabels track by $index" dashboard-table all-labels="properLabels" all-years="properYears" all-data="properData"></tr>
更改为此
<tr class="dashboard-table-row" ng-repeat="labels in properLabels track by $index" dashboard-table all-labels="{{properLabels[$index}}" all-years="properYears" all-data="{{properData[$index]}}"></tr>
在传递数组的位置添加[$index]
,只会将等于数组中N个索引位置的值发送到我的指令
希望这对未来的人们有所帮助!我只是想,在安静地搜索了一段时间后,我找到了如何做到这一点……当我做到时,all number=“someNumbers[$index]”
它似乎一次只发送一个值。这是对我自己问题的回答还是这样做是错误的?@Daniel Nalbach你向这个方向迈出了一步!我想你是在试图实现这个{num}num应该等于somenumes[$index]并且可用于ng repeat元素或任何嵌套元素的任何属性要澄清,num不是索引位置,而是作为独立模型的索引位置的值。如果我理解正确,您希望使用从提供的数组中选择的随机数生成行。不是吗?