Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire button click in AngularJS

I want to fire a button's click event when pressing ENTER inside a input and I find it quite difficult with AngularJS.

My view (simplified, updated):

<!doctype html>
<html lang="en" ng:app="test">
    <head>
        <meta charset="utf-8" />
        <title>Test</title>
        <link rel="stylesheet" href="css/app.css" />
    </head>
    <body ng-controller="TestController">
        <button ng-click="onButton1Click()" class="btn1">Click Me</button>
        <button ng-click="onButton2Click()" class="btn2">Don't click me</button>
        <script src="lib/jquery/jquery.js"></script>
        <script src="lib/angular/angular.js"></script>
        <script src="js/testcontroller.js"></script>
    </body>
</html>

My controller for this view:

'use strict';

angular.module('test', [])
.controller('TestController', ['$scope',
    function($scope) {

        $scope.onButton1Click = function() {
            alert("Hello");
        }

        $scope.onButton2Click = function() {
            $('.btn2').click();
        }
}])

I simplified all the code to this. When I click on btn2 I get this error

$apply already in progress

No, I can't call $scope.onButton1Click() directly, I must simulate the btn1 click.

like image 398
ali Avatar asked Apr 18 '13 10:04

ali


People also ask

How to trigger click event in AngularJS?

The syntax is the following: function clickOnUpload() { $timeout(function() { angular. element('#myselector'). triggerHandler('click'); }); }; // Using Angular Extend angular.

What is ng-click in AngularJS?

The ng-click directive tells AngularJS what to do when an HTML element is clicked.

How to call ng-click function from JavaScript?

Any field can be updated with ng-click using a custom JavaScript function. For this, we can make a clickable object in HTML (usually a button) and attach an ng-click directive with it that calls this custom function. The ng-click Directive in AngluarJS is used to apply custom behavior when an element is clicked.

What is an angular element?

Angular elements are Angular components packaged as custom elements (also called Web Components), a web standard for defining new HTML elements in a framework-agnostic way.


2 Answers

You mentioned

fire a button's click event when pressing ENTER inside a input

So if it is safe to assume that you can have a form I would prefer using ng-submit as shown below.

<form ng-submit="clickEventFunction()">
  <input type="text"/>
  <button type="submit">Click</button>
</form>

Note button type should be submit.

like image 99
skeep Avatar answered Oct 15 '22 11:10

skeep


This took me a while to figure out (lots of fiddles).

<form id="nowsorting" ng-submit="getData(sc_user)">Now sorting the Soundcloud likes of <input type="text" ng-model="sc_user"><input type="submit" value="Sort"></form>

Make a form and use ng-submit to fire the event (likely a function).

Then create two inputs (one is "text" and the other "submit").

Then pushing enter should fire the ng-submit event/function.

like image 38
Kyle Pennell Avatar answered Oct 15 '22 11:10

Kyle Pennell