使用来自ng-click指令的警报

AME*_*AME 19 angularjs

angularjs新手.想要将表达式写入ng-click.

例:

x.directive('li',function(){
  return {
      restrict: 'E',
      replace: true, 
      template: '<games> <game  ng-click="(alert({{ game }})" ng-repeat="game in games"> {{ game.team1 }} {{game.bets }}   <game></br></games> '
  }     
});
Run Code Online (Sandbox Code Playgroud)

我想点击提醒游戏,但是我收到了这个错误:

Error: [$parse:syntax] Syntax Error: Token 'game' is unexpected, expecting [:] at column 11 of the expression [(alert({{ game }})] starting at [game }})].
Run Code Online (Sandbox Code Playgroud)

Nit*_*eli 42

当您从ng-click请求"alert"时,它会在$ scope上查找该方法,但它不在那里.

请参阅此plunkr,我在示波器上使用了一个函数,在单击该指令时调用警报.

在控制器中我们设置功能:

$scope.test = function(text) {
  alert(text);
}
Run Code Online (Sandbox Code Playgroud)

或者你可以这样做:$scope.alert = alert.bind(window);.如果你这样做的话,如果没有将上下文绑定到窗口,它将无法工作.

在指令的ng-click中,我们调用了我们的函数:

 ng-click="test(game)"
Run Code Online (Sandbox Code Playgroud)

  • 为你的时间+1,但这不是我要求的. (2认同)