我想用ng-model="model.ind[$index]"
来记录哪个标签是活动的,然后当我点击标签(a标签)时,parentIndex和$ index将被传递给我的控制器,而model.ind[$index]
将是设置等于当前索引。
但是,在我的功能选择中,我无法获取this.model.tag[parentIndex]
并收到错误TypeError: Cannot read property '0' of undefined
at angular.module.component.controller.select
我该怎么做才能解决这个问题?
我的组件是:
<div class="filters-wrapper">
<div class="filters-section" ng-repeat="tag in $ctrl.tagList track by $index">
<p ng-model="model.ind[$index]" ng-init="model.ind[$index] = 0; parentIndex = $index" >{{tag.name}}</p>
<a ng-repeat="t in tag.tags track by $index" class="filter"
ng-class="{active:$index == model.ind[parentIndex]}"
ng-click="$ctrl.select(parentIndex,$index)"
>
{{t}}
</a>
</div>
</div>
我的组件js是:
angular.module('myApp').component('filterBar',{
templateUrl: 'component/filter-bar/filter-bar.html',
controller: ['$log',function filterBarCtrl($log){
this.select = function(parentIndex,index){
this.model.ind[parentIndex] = index;
};
this.selected = 0;
this.model = {};
this.tagList = [
{
name: 'type',
tags:["a","b"]
}
];
}]
});
答案 0 :(得分:0)
您需要在模板中使用$ctrl.model
将其与控制器字段绑定。如果您想在模板
this.model.ind
,则必须定义$ctrl.model.ind[...]