I have two buttons that are using the same ng-click with different params.
<label class="item item-input">
<button ng-click="takePicture(true)">Save Settings</button>
<button ng-click="takePicture(false)">Choose from Gallery</button>
</label>
No matter what I do, the buttons pass the same param as what is in the first function call.
With a simple controller function for testing, the same param gets logged. In this case, it is true
for both.
$scope.takePicture = function(my_param) {
console.log(my_param);
}
These seems to happen only in Ionic, not with standard Angular. Here is a CodePen for a working example.
http://codepen.io/anon/pen/JYBKVQ
Edit: Per the solution below, I've included the source of the problem in the code excerpt above. Curse you <label>
, curse you to heck!
In this article, we will learn how to get many/multiple functions to the ng-click directive passed, in just one click. The key is to add a semi-colon (;) or a comma (,). All the functions must be separated by a (;) or a (, ). This syntax is supported by all the elements in the HTML.
Another significant difference between ng-click and onclick is the execution context. Code inside an onclick attribute executes against the global window object, while an expression inside of ng-click executes against a specific scope object, typically the scope object representing the model for the current controller.
We can add ng-click event conditionally without using disabled class.
The ng-click directive tells AngularJS what to do when an HTML element is clicked.
It is an issue with nesting the buttons inside a label. If you remove the label it works as expected: http://codepen.io/anon/pen/ojMzLj
<div class="list list-inset">
<h3>Profile Picture</h3>
<button class="button button-block button-calm" ng-click="takePicture(true)">Save Settings</button>
<button class="button button-block button-calm" ng-click="takePicture(false)">Choose from Gallery</button>
<label class="item item-input">
<input type="text" placeholder="Circle Color" ng-model="data.color">
</label>
<label class="item item-input">
<input type="text" placeholder="Stroke Color" ng-model="data.stroke">
</label>
</div>
Indeed the issue is with the label as @DenimChicken pointed out, other tags work as expected.
I took a look at: http://www.w3schools.com/tags/tag_label.asp
I also tested some other cases and it turns out, the label is passing clicks to the first button it finds, in fact it ALWAYS calls the first ng-click, no matter which one you click. The label find the first intractable element, and stops the other events from propagating. This can be changed with the "for" attribute.
When the label is targeted, this happens, the target always gets clicked.
<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>
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