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.
$scope.triggerClick = function () {
$timeout(function() {
angular.element('#foo').trigger('click');
}, 100);
};
The $timeout will run an $apply to the cycle if necessary.
$timeout(function() {
angular.element(domElement).triggerHandler('click');
}, 0);
The $timeout
is for breaking angular's $apply
cycle.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With