如何在ngRepeat(AngularJS)中重复单击处理程序?

时间:2016-11-27 23:32:12

标签: javascript angularjs angularjs-ng-repeat

我有以下问题。我想重复菜单链接。但该程序没有创建相应的点击处理程序。

查看:

//view is created by Angular Material
<md-menu-item ng-repeat="mItem in ::menuItems">
    <md-button ng-click="mItem[action]">
        <md-icon>{{ ::mItem.icon }}</md-icon>
        {{ ::mItem.name }}
    </md-button>
</md-menu-item>

控制:

$scope.menuItems = [
        { icon: 'mode_edit', name: 'Edit', action: 'clickEdit()' },
        { icon: 'delete', name: 'Delete', action: 'clickDelete()' }
    ];

$scope.clickEdit = clickEdit;
$scope.clickDelete = clickDelete;

    function clickEdit() {
        $log.info('edit clicked');
    }

    function clickDelete() {
        //code...
    }

在ngClick中获取处理程序需要做些什么?

1 个答案:

答案 0 :(得分:1)

我认为将这种逻辑保留在这样的服务中是最好的:

//view is created by Angular Material
<md-menu-item ng-repeat="mItem in ::menuItems">
    <md-button ng-click="action(mItem.action)">
        <md-icon>{{ ::mItem.icon }}</md-icon>
        {{ ::mItem.name }}
    </md-button>
</md-menu-item>

$scope.menuItems = [
    { icon: 'mode_edit', name: 'Edit', action: 'edit' },
    { icon: 'delete', name: 'Delete', action: 'delete' }
];

$scope.action = function(actionName){
   // call service
}