Angularjs 如何将视图中的模型数据解析为视图中的javascript函数

Angularjs 如何将视图中的模型数据解析为视图中的javascript函数,angularjs,Angularjs,假设我具备以下条件: <button ng-repeat="{{item in items}}" ng-click="{{item.name}}Function()">{{item.name}}</button> 等等。我将移动用于确定要调用javascript的函数的逻辑 html 编辑 如果要避免切换,可以通过以下方式进行: html 我会避免创建调用其他函数的不必要函数。如果$scope引用了以下函数: $scope.items = [ { name: 'f

假设我具备以下条件:

<button ng-repeat="{{item in items}}" ng-click="{{item.name}}Function()">{{item.name}}</button>

等等。

我将移动用于确定要调用javascript的函数的逻辑

html

编辑

如果要避免切换,可以通过以下方式进行:

html


我会避免创建调用其他函数的不必要函数。

如果
$scope
引用了以下函数:

$scope.items = 
[
  { name: 'firstItemFunction' },
  { name: 'secondItemFunction' }
];

$scope.firstItemFunction = function () {
  console.log('firstItemFunction');
};

$scope.secondItemFunction = function () {
  console.log('secondItemFunction');
};
HTML:

以及:


演示:

两个答案都是正确的!tasesKATT的答案非常详细,并给出了各种备选方案,但我最终使用了Mattew-Beng方法(使用开关而不是if..但总体思路相同)。顺便说一句。。您的“更多”是正确的,因为您使用“this[item.name]()”将名称合并为动态函数名。我正在构建一个屏幕,您可以选择多个社交网络进行身份验证,每个社交网络显然有一个相似但不同的身份验证过程,因此在html中没有10个不同的函数,我只有一个SocialIn(networkname),然后让后端决定做什么。。我认为这样做比直接在前端执行facebookLogin()、twitterLogin()更优雅..我最终实现了这个,但使用了一个开关。我的主要问题是关于“item”而不是{{item}}在click函数中的实际用法。
<button ng-repeat="{{item in items}}" ng-click="figureOutFunction(item)">{{item.name}}</button>
$scope.figureOutFunction(item){
    if(item.name == "firstItem"){
        firstItemFunction();
    }
    else if(item.name == "secondItem"){
        secondItemFunction();
    }
};
<button ng-repeat="{{item in items}}" ng-click="item.action()">{{item.name}}</button>
var firstItemFunction = function(){
    ...
};
var secondItemFunction = function(){
    ...
};

$scope.items = [{
  name: 'firstItem',
  action: firstItemFunction
}, {
  name: 'secondItem',
  action: secondItemFunction
}];
$scope.items = 
[
  { name: 'firstItemFunction' },
  { name: 'secondItemFunction' }
];

$scope.firstItemFunction = function () {
  console.log('firstItemFunction');
};

$scope.secondItemFunction = function () {
  console.log('secondItemFunction');
};
<button ng-repeat="item in items" ng-click="this[item.name]()">
  {{item.name}}
</button>
$scope.items = [{
  name: 'firstItemFunction'
}, {
  name: 'secondItemFunction'
}];

$scope.firstItemFunction = function() {
  console.log('firstItemFunction');
}

$scope.secondItemFunction = function() {
  console.log('secondItemFunction');
}

$scope.execute = function(action) {
  $scope[action]();
};
<button ng-repeat="item in items" ng-click="execute(item.name)">
  {{item.name}}
</button>
$scope.items = [{
  name: 'firstItemFunction'
}, {
  name: 'secondItemFunction'
}];

$scope.execute = function(action) {
  $window[action]();
};