Angular-js: ng-click doesn't work


Shiva

Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file:

    <html ng-app="MyApp">

<head>
    <title>Angular Demo App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
    <script src="app.js"></script>
</head>

<body>
    <div ng-controller="MyCtrl as ctrl">
        <h1>My Calculator</h1>
        <input type="text" ng-model="ctrl.input1"></input>
        <span ng-bind="this.selectedOperation"></span>
        <input type="text" ng-model="ctrl.input2"></input>
        <button ng-click="ctrl.computeResult()">=</button>
        <span ng-bind="ctrl.resultValue"></span>
        <p>Choose the operation:
            <button ng-click="ctrl.buttonClicked('+')">+</button>
            <button ng-click="ctrl.buttonClicked('-')">-</button>
            <button ng-click="ctrl.buttonClicked('*')">*</button>
            <button ng-click="ctrl.buttonClicked('/')">/</button>
        </p>
    </div>
</body>

</html>

Here is the controller:

    var app = angular.module("MyApp", []);
app.controller("MyCtrl", MyCtrl);

function MyCtrl() {

    this.buttonClicked = function (button) {
        this.selectedOperation = "button";
    }
    this.computeResult = function () {
        var num1 = parseFloat(this.input1);
        var num2 = parseFloat(this.input2);

        if (selectedOperation === "+") {
            this.resultValue = num1 + num2;
        } else if (selectedOperation === "-") {
            this.resultValue = num1 - num2;
        } else if (selectedOperation === "*") {
            this.resultValue = num1 * num2;
        } else if (selectedOperation === "/") {
            this.resultValue = num1 / num2;
        }

    }

}
Sayetaran

You need to use instead ofthis.selectedOperationselectedOperation

if (this.selectedOperation === "+") {
  this.resultValue = num1 + num2;
}

Your function should also assign parameters,

 this.buttonClicked = function (button) {
        this.selectedOperation = button;
    }

demo

var app = angular.module("MyApp", []);
app.controller("MyCtrl", MyCtrl);
function MyCtrl() {
   
    this.buttonClicked = function (button) {
        this.selectedOperation = button;
    }
    this.computeResult = function () {
        var num1 = parseFloat(this.input1);
        var num2 = parseFloat(this.input2);
        if (this.selectedOperation === "+") {
            this.resultValue = num1 + num2;
        } else if (this.selectedOperation === "-") {
            this.resultValue = num1 - num2;
        } else if (this.selectedOperation === "*") {
            this.resultValue = num1 * num2;
        } else if (this.selectedOperation === "/") {
            this.resultValue = num1 / num2;
        }

    }

}
<html ng-app="MyApp">
<head>
    <title>Angular Demo App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular.min.js"></script>
</head>
<body>
    <div ng-controller="MyCtrl as ctrl">
        <h1>My Calculator</h1>
        <input type="text" ng-model="ctrl.input1">
        <span ng-bind="ctrl.selectedOperation"></span>
        <input type="text" ng-model="ctrl.input2">
        <button ng-click="ctrl.computeResult()">=</button>
        <span ng-bind="ctrl.resultValue"></span>
        <p>Choose the operation:
            <button ng-click="ctrl.buttonClicked('+')">+</button>
            <button ng-click="ctrl.buttonClicked('-')">-</button>
            <button ng-click="ctrl.buttonClicked('*')">*</button>
            <button ng-click="ctrl.buttonClicked('/')">/</button>
        </p>
    </div>
</body>

</html>

Related


Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

Angular JS ng-click doesn't work in child element

Tantrop I'm trying to toggle a variable when an element in the DOM is clicked, but I'm getting some weird behavior. Full example is here Essentially, if I click ng on the .options div and then leave the controller on the .options-tab div, the event fires (but

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

Angular JS ng-click doesn't work in child element

Tantrop I'm trying to toggle a variable when an element in the DOM is clicked, but I'm getting some weird behavior. Full example is here Essentially, if I click ng on the .options div and then leave the controller on the .options-tab div, the event fires (but

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Angular JS: ng-click scope set doesn't work in ng-if

Jeffrey Today, I saw the error in angularjs: When you try to set scope value directly in ng-click, it doesn't work if ng-click tests the same scope value in ng-click -> http://jsfiddle.net/9j2TL/ 26 / angular.module('test', []) .controller('testCtrl', function

Angular JS - ng-click doesn't work in select option [Chrome]

Joe 82 I have an select optionelement whose purpose is to change the way an ordered list is sorted. Every time I choose another option, it firesng-click <select> <option ng-click="sortType = ['-grade','-year']">Grade</a> <option ng-click="sortType =

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular toggle variable in ng-click doesn't work

amnesia I have a table that lists customers, and for customers that also have a list of customers, the user can click on the table row to display that list. HTML looks like this: <tbody ng-repeat ="item in customers | clientSettingsFilter:customerId">

ng-click doesn't work in angular-ui-grid

username I'm trying to fire a click event when a cell in ui-grid is clicked but ng-click is not working. Also ng-class doesn't work, I can't get width: "*" works fine. Does anyone know any accurate documentation for angular-ui-grid ? I am using angular v 1.5.1

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular toggle variable in ng-click doesn't work

amnesia I have a table that lists customers, and for customers that also have a list of customers, the user can click on the table row to display that list. HTML looks like this: <tbody ng-repeat ="item in customers | clientSettingsFilter:customerId">

Calling function with angular ng-click doesn't work

Program 345 I am trying to switch the view from one page to another when a button is pressed. I'm using ng-click but it doesn't switch views when the button is pressed .js file $scope.onCreateAppointment = function () { $scope.templates.myTempla