Javascript 使用$eval将参数传递回函数
我试图利用角度引导组件,特别是tab组件。我的自定义指令当前如下所示:Javascript 使用$eval将参数传递回函数,javascript,angularjs,Javascript,Angularjs,我试图利用角度引导组件,特别是tab组件。我的自定义指令当前如下所示: scope: { select: '&select' }, template: '<tabset>' + '<tab ng-repeat="tab in menu.tabs" disabled="tab.disabled" active="tab.active" selec
scope: {
select: '&select'
},
template: '<tabset>' +
'<tab ng-repeat="tab in menu.tabs" disabled="tab.disabled" active="tab.active" select="tabSelected($index)">' +
'<tab-heading>' +
'<i class={{tab.headingClass}}></i> {{tab.title}}' +
'</tab-heading>' +
'<div ng-include="tab.partial"></div>' +
'</tab>' +
'</tabset>',
controller: 'TabBarController',
My html removation.html使用指令及其select属性从其自己的controller removation-controller.js传入函数,供上述函数使用:
$scope.tabSelected = function (tabIndex) {
if ($scope.select){
$scope.$eval( $scope.select({index : tabIndex}) );
}
};
因此,如果select接收到一个函数,它会检查它,并假设它会传回制表符索引——问题是我总是返回未定义的。下面是我制作链接的html和需要选项卡索引的删除控制器:
<div tab-bar file-url="..."
select="removalModel.onSelected()"
></div>
$scope.removalModel.onSelected = function(tabIndex){
cvsBoxService.resetScrollToTop('planoRemovalContainer');
// Always gets back undefined
console.log( "Tab index:"+tabIndex );
};
选择calls removalModel.onSelected。您不会将任何参数传递给onSelected,因此tabIndex将始终是未定义的
您需要类似removalModel.onSelectedindex的内容
我不知道为什么要使用$scope.$eval.这有点令人困惑,但我马上看到的是,在HTML中使用$index,而在代码中调用select时使用index。我知道这很难正确解释-是的,我正在创建一个JSON对象来保存索引的值
<div tab-bar file-url="..."
select="removalModel.onSelected()"
></div>
$scope.removalModel.onSelected = function(tabIndex){
cvsBoxService.resetScrollToTop('planoRemovalContainer');
// Always gets back undefined
console.log( "Tab index:"+tabIndex );
};
$scope.$eval($scope.select(), {index : tabIndex});