Home » Angularjs » Use alert from ng-click of a directive

Use alert from ng-click of a directive

Posted by: admin November 30, 2017 Leave a comment


New to angularjs.
Want to write an expression into an ng-click.


  return {
      restrict: 'E',
      replace: true, 
      template: '<games> <game  ng-click="(alert({{ game }})" ng-repeat="game in games"> {{ game.team1 }} {{game.bets }}   <game></br></games> '

I want to alert the game on click but I got this error:

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

When you ask for ‘alert’ from ng-click, it looks for that method on the $scope, and it’s not there.

See this plunkr where I used a function on the scope to call the alert when the directive is clicked.

In the controller we set the function:

$scope.test = function(text) {

Or you can just do: $scope.alert = alert.bind(window);. It won’t work without binding the context to the window if you do it like that.

In the directive’s ng-click we call our function: