【问题标题】:AngularJS set ng-click function with scope variables within ng-repeatAngularJS 在 ng-repeat 中使用范围变量设置 ng-click 函数
【发布时间】:2014-12-12 03:35:11
【问题描述】:

我想在 ng-repeat 中为 ng-click 分配不同的功能:

<button ng-repeat="button in notification.buttons" ng-click="button.fn"> {{button.label}} </button>

控制器:

app.controller('MainCtrl', ['$scope', function($scope){
    $scope.notification = {
        ctrl: 'logout', 
        text: 'Logout?',
        buttons: [
                {label:'Yes', fn: 'logout()'},
                {label:'No', fn: 'cancel()'},
        ],
    };
}]);

app.controller('logout', ['$scope', function($scope){
    $scope.logout = function(){
        alert('logout');
    };
    $scope.cancel = function(){
        alert('cancel');
    };
}]);

如果我将 button.fn 放在双花括号 ng-click="{{button.fn}}" 中,生成的 html 在 Chrome 检查器中看起来不错,但 Angular 会抛出以下错误:

错误:[$parse:syntax] 语法错误:令牌 'button.fn' 是意外的, 在表达式 [{{button.fn}}] 开始的第 3 列期待 [:] 在 [button.fn}}]。

如果没有双花括号,则没有错误,但 Angular 会像这样生成 html,并且函数不会触发:

 <button ng-repeat="button in notification.buttons" ng-click="button.fn" class="ng-binding ng-scope"> Yes logout()</button>

PLUNKER

我有一个关于使用范围变量设置 ng-controller 的相关问题,这个例子与 here 相同。

【问题讨论】:

  • ng-click 期待一个函数(IE button.fn()),而不是你得到的指针。这就是它不会火的原因。我见过的唯一选择是创建一个指令。见This

标签: angularjs angularjs-ng-repeat angularjs-ng-click


【解决方案1】:

http://plnkr.co/edit/QE50kpfBjXy2WPvjVsJc?p=preview

我已经为您的代码创建了一个分支。您的“fn”引用是字符串。我将它们更改为函数,并将“()”添加到模板中的“button.fn”中。我还更改了函数引用的位置,因为函数的定义位于不同的控制器中。

var app = angular.module('plunker', []);

app.controller('MainCtrl', ['$scope', function($scope){
    $scope.notification = {
        ctrl: 'logout', 
        text: 'Logout?',
        buttons: [
    			{label:'Yes', fn: null},
    			{label:'No', fn: null},
        ],
    };
}]);

app.directive('notification', [
	function() {
		return {
		restrict: 'E', // E = Element, A = Attribute, C = Class, M = coMment
		templateUrl: 'notification.tpl.html',
		replace: true,
		link: function(scope, element, attrs) {
      //...
		}
	};
		
		
}])

app.controller('logout', ['$scope', function($scope){
  $scope.logout = function(){
      alert('logout');
  };
  $scope.cancel = function(){
      alert('cancel');
  };
  
  $scope.notification.buttons[0].fn = $scope.logout;
  $scope.notification.buttons[1].fn = $scope.cancel;
}]);
<div ng-controller="logout">
    <p>{{notification.text}}</p>
    <button ng-repeat="button in notification.buttons" ng-click="button.fn()"> {{button.label}} {{button.fn}}</button>
    <button ng-click="logout()"> Test</button>
</div>

【讨论】:

  • 嗯.. 我看到您将这些功能移到了主控制器中。实际上这只是解决方案的一半,因为我想从内部(注销)控制器调用函数......
  • 您能否更新您的答案以调用原始控制器中的函数?
  • MainCtrl 控制器中对 logout() 和 cancel() 的引用在注销控制器中定义。那是个问题。 MainCtrl 永远不会知道层次结构中更深层次的定义。我将不得不让登录控制器在按钮数组中设置函数引用以使其工作。这是一个新的plunkr。 plnkr.co/edit/QE50kpfBjXy2WPvjVsJc?p=preview
  • 是的,我一直在深入挖掘parent and child scope hierarchy。这是一个非常有趣的解决方法,谢谢分享!
猜你喜欢
  • 2013-07-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多