用ng repeat调用函数

Call a function in ng-repeat

本文关键字:函数 调用 repeat ng      更新时间:2023-09-26

我有一个数组,其中包含一些按钮的名称和单击按钮时要调用的function的名称。

如果我通过按钮ng-repeat,除了功能没有执行之外,一切都很好。我不确定我还能尝试什么,甚至不确定这是否可能。

这是一些数据

$scope.something = [{
    name: 'Cool Button',
    func: 'test'
}, {
    name: 'Another button',
    func: 'something'
}]

我是这样使用ng-repeat的。

<button ng-click="some.func" ng-repeat="some in something">{{some.name}}</button>

以下是我试图使函数工作的内容。

some.func // Nothing happens
some.func() // Throws a error
{{ some.func }}() // Nothing Happens

这里有一个函数,它被称为

$scope.test = function() {
    alert('clicked');
};

这是否有可能?

我做了一个快速的小提琴

ng-click="this[some.func]()"

或者直接引用函数:

$scope.something = [{
    name: 'Cool Button',
    func: $scope.test
}, {
    name: 'Another button',
    func: $scope.anotherFunc
}]
ng-click="some.func()"