How to trigger a "click" event in Angular js

how to port lower jquery function to angular js?

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

The problem is that I plan to automatically launch the submit button when the user fills in some data in our application.

since im is from jquery background,

early.

+6
source share
5 answers

Usually you do not submit the form in AngularJS. You send data using XHR and get a response in JSON.

Something like that:

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 .

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

If necessary, $ timeout will start $.

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

$timeout is for breaking the angular $apply loop.

+7
source

Instead, you can use ng-change and call the send function from your controller. Something like that:

 <input type="text" ng-model="userData.field1" ng-change="mySubmitFunction(userData)"> 
-1
source

If you have a f.eks button, you need to use ng-click="myFunctionName()" on the button itself.

And in the script file you use myFunctionName = $scope.function(){ yourCode... }

If you have taken care of a completely new one for Angular ... you should read a little about it, since it basically stays away from the DOM, takes β€œcontrol” of your web page and needs ng-app , ng-controller and uses $scope for storing states for content and data.

-2
source

Source: https://habr.com/ru/post/973147/


All Articles