Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger 'click ' event in Angular js

how to migrate the below function of jquery to angular js ?

  $( "#foo" ).trigger( "click" );

Problem here is im planning to trigger submit button automatically when user fills some data in our application.

so as im from jquery background ,

thanks in advance.

like image 870
praveenpds Avatar asked Jul 31 '14 07:07

praveenpds


3 Answers

$scope.triggerClick = function () {
  $timeout(function() {
    angular.element('#foo').trigger('click');
  }, 100);
};

The $timeout will run an $apply to the cycle if necessary.

like image 107
Ferox Avatar answered Oct 26 '22 11:10

Ferox


$timeout(function() {
    angular.element(domElement).triggerHandler('click');
}, 0);

The $timeout is for breaking angular's $apply cycle.

like image 28
Gerben Rampaart Avatar answered Oct 26 '22 10:10

Gerben Rampaart


Usually you don't submit a form in AngularJS. You send the data using an XHR and get a response in JSON.

Something like this:

VIEW

<form name="myForm" ng-submit="login(credentials)">

  <label>
    Username:
    <input type="text" ng-model="credentials.username" />
  </label>

  <label>
    Password:
    <input type="password" ng-model="credentials.password" />
  </label>

 <button type="submit">Login</button>

</form>

CONTROLLER

$scope.credentials = {};
$scope.login = function login(credentials) {
  var user = credentials.username;
  var pass = credentials.password;

  // Do some data validation
  if (!user || !pass) {
    $window.alert('Please, enter a username and a password!');
    return;
  }

  // Send the data and parse the response
  // (usually done via a Service)
  LoginService.login(user, pass);
};

See, also, this short demo.

like image 38
gkalpak Avatar answered Oct 26 '22 11:10

gkalpak