Question

In the following code sample is there a 'Angular way' of preventing the default event of a button click or form submit. Currently I'm using 'onsubmit' to accomplish the task.

<form onsubmit="return false;">
  <input ng-model="ctrl.task">
  <button class="btn btn-primary" ng-click="ctrl.addTask()">Add</button>
</form>
Was it helpful?

Solution

Change your html to use $event like this (ctrl removed because controllers have been removed from Angular.dart):

<form onsubmit="return false;">
  <input ng-model="task">
  <button class="btn btn-primary" ng-click="addTask($event)">Add</button>
</form>

And in your component class:

void addTask(MouseEvent evt){
  evt.preventDefault();
}

OTHER TIPS

<form onsubmit="return false;">
  <input ng-model="ctrl.task">
  <button class="btn btn-primary" ng-click="ctrl.addTask($event)">Add</button>
</form>

In the controller:

$scope.ctrl = function() {
  addTask: function(event) {
    event.preventDefault();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top