Button ng-click with identical function parameters

I am facing an issue with two buttons that have the same ng-click but different parameters.

<label class="item item-input">
  <button ng-click="takePicture(true)">Save Settings</button>
  <button ng-click="takePicture(false)">Choose from Gallery</button>
</label>

Despite my efforts, both buttons end up passing the same parameter as the first function call.

Even with a simple controller function for testing purposes, the same parameter is logged. In this instance, it always logs true.

$scope.takePicture = function(my_param) {
  console.log(my_param);
}

This issue seems to be specific to Ionic framework and does not occur with standard Angular. A CodePen example demonstrating the problem can be found here:

http://codepen.io/anon/pen/JYBKVQ

Edit: As suggested in the solution below, the source of the problem lies within the code excerpt above. The culprit appears to be the <label> element. Beware!

Answer №1

The issue arises when nesting buttons inside a label. By removing the label, the functionality works as intended: http://codepen.io/anon/pen/ojMzLk

<div class="list list-inset">
    <h3>User Avatar</h3>
    <button class="button button-block button-calm" ng-click="takePhoto(true)">Save Changes</button>
    <button class="button button-block button-calm" ng-click="takePhoto(false)">Choose from Library</button>
    <label class="item item-input">
        <input type="text" placeholder="Profile Image Color" ng-model="info.color">
    </label>
    <label class="item item-input">
        <input type="text" placeholder="Border Color" ng-model="info.border">
    </label>
</div>

Answer №2

It appears that the issue lies within the label, as pointed out by @DenimChicken, while other tags function properly.

After reviewing the information available at: http://www.w3schools.com/tags/tag_label.asp

I conducted further tests and discovered that the label is triggering the first ng-click it encounters, regardless of the button selected. The label identifies the first interactive element and prevents any additional events from occurring. This behavior can be altered using the "for" attribute.

Whenever the label is specified, its target will always receive the click event.

<label for="false" >
        <h3>Profile Picture</h3>
        <button class="button button-block button-calm" ng-click="takePicture(true)">Save Settings</button>
        <button id="false" class="button button-block button-calm" ng-click="takePicture(false)">Choose from Gallery</button>
</label>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Calculating the total value in a Laravel database

Is there a way for me to calculate the number of apartments in a project based on the floor they are located on? This is db $table->id(); $table->unsignedBigInteger('project_building_id')->nullable(); $table->unsignedBigInteger(&apos ...

When using Axios to GET from a local PHP file, it only retrieves the code instead of running

I've run into an issue accessing the API as it has CORS disabled, requiring me to make requests on the server side. Currently, I'm using React and Axios to send a GET request to a local php file that should trigger cURL execution. However, instea ...

Guide on how to efficiently navigate and extract data from a (local) XML file using Prototype JS

I'm currently working on a project that already utilizes PrototypeJS and I need to develop a module for it. Here's what I have: - An XML file containing the necessary information Here's what I'm aiming for: - A basic div that showcase ...

Message displayed within Ng-repeat loop

Having trouble implementing a tooltip within an ng-repeat for each item in a td element. Whenever the mouse hovers over an item inside the td, I want a tooltip to display with more details. The code below shows my setup, using ng-if to prevent displaying e ...

Mastering the art of raycasting onto a point cloud in ThreeJs R71

I am currently working with Autodesk Forge, leveraging Three.js r71, and I am looking to implement a raycaster to identify clicks on various elements within a point cloud. If anyone could provide a sample code snippet on how to achieve this using Three.js ...

Guide on submitting a form using a custom AJAX function based on the id or class parameter

Instead of writing an ajax call every time with changing API URL, post method, and form id or class name based on the page, I am attempting to develop a custom function that can manage the API call based on the provided parameters. The essential parameters ...

Utilizing Bootstrap Modal functionality in a Django web application to enhance user experience

I am currently facing a minor issue in my Django application. I am attempting to utilize a modal from Bootstrap 4 along with AJAX to create a new object. Below you can view the code I used. However, when the user clicks the button, instead of seeing the mo ...

Tips for eliminating duplicate values from an array

In my NodeJS code, I am currently comparing values in an array. The issue at hand is: I start with an empty array: var items = []; then proceed to insert some values into it: items[0] = {a:1, b:222}; items[1] = {a:1, b:333}; items[2] = {a:1, b:222}; ...

What are some methods for embedding HTML content onto a specific section of a webpage in a Node.js + Express application, without displaying it as the

Objective My goal is to insert a portion of HTML into a web page. Issue The page is not displaying properly with the desired styling and layout. I have a specific page that must be written in plain HTML rather than Jade, although I will still be usin ...

Tips for customizing table cell styling in editable cells with React material table

My code utilizes a material table with editable cells. However, I am encountering a strange style issue when I edit a cell in the table. Please refer to the image below. Can anyone suggest a solution to fix this problem? https://i.sstatic.net/Miiov.png ...

Interacting with wpdb using AngularJS

I just started learning AngularJS and I'm eager to implement it on my WordPress website. My goal is to display a table with data from a database in my WordPress site, but I am encountering difficulties accessing the WordPress functions and variables. ...

The Nuxt image keeps disappearing every time I navigate to a new page

Whenever I have an image displayed on my Nuxt page and then navigate away from it, the image breaks and I can't figure out why. This is what my code looks like: <img :src="baseUrl + 'storage/'+ front.featured_image" alt="p ...

Increasing the variable by 1 in PHP will result in the variable value being incremented to 1

My issue involves incrementing a variable in my .php file code that changes the value in the database. After incrementing the acc_points variable by one, it updates the data in the MySQL database and then returns the data to the JavaScript, which alerts th ...

With jQuery's .text() method, it is possible to modify the first span's Bootstrap class, but not the

As someone who is new to javascript, html, and jquery, I have been trying to change the text of 2 span elements in the same div using jquery's .text() command. Despite going through various solutions provided by different questions, none seem to work ...

Can you explain the significance of this regular expression?

I'm trying to decipher the meaning of this regular expression. Can anyone help? "^[A-Z]{3}-[4-7]\d{2,4}\$$" My understanding is that it must start with exactly 3 uppercase letters and end with a sequence of 2, 3, or 4 digits (although I a ...

What is the best way to receive a callback when a user cancels a dialog box to choose a mobile app after clicking on

I am currently exploring HTML coding for mobile devices and looking to integrate map routing functionality. When it comes to mobile devices, utilizing the local app is the more practical option, and I have had success implementing this by modifying the l ...

Utilize CSS styling on dynamically loaded HTML content

I am working on a project similar to the StackOverflow editor. I am fetching markdown text, converting it to HTML, and then displaying it in a preview area. However, when I try to style the injected HTML elements using CSS, the styles are being ignored. Up ...

Ways to remove specific characters from the escape() function in express-validators

When using the check method from express-validator to validate user input, I'm curious if there's a way to exclude certain characters from the test. For example, currently I have: check("profile.about").trim().escape() This code snippet convert ...

Angular Bootstrap UI - Ensuring only one element collapses at a time

I have integrated the angular bootstrap UI library into my website by following this link: https://angular-ui.github.io/bootstrap/ One issue I am facing is that when I implement a collapsible feature using this library, it collapses or expands every eleme ...

What is the best method for combining multiple text box values into a single text box?

I need assistance with concatenating multiple text box values into one dynamic text box. I am currently retrieving values using IDs, which are dynamically added as the text boxes are dynamically created. However, when using a for loop to dynamically add te ...