Ng-submit and ng-click fires many times i ionic iPhone app - iphone

I'm trying out the ionic framework and it looks really nice. However, I have a problem with form submission: the form fires twice. First when the submit button is pressed, and then if I just tap anywhere on the screen. This happens both in the xcode simulator and on my iphone 4gs.
This is what I have done:
I install the sidemenu template with: ionic start myApp sidemenu.
I then simply paste this form in to the tab-dash template:
<form ng-submit="createTask(task)">
<div class="list">
<label class="item item-input">
<input type="text" placeholder="What do you need to do?" ng-model="task.title">
</label>
</div>
<div class="padding">
<button type="submit" class="button button-block button-positive">Create Task</button>
</div>
</form>
And in my controller I simply have:
$scope.createTask = function(task) {
alert(task.title);
};
This is the only change I made to the starter template, and still the form submits twice. I have no idea why. Would really appreciate some guidance here!

Remove the type="submit" from the button and remove the ng-submit from the form and move it as a ng-click on the button itself.
So you should end up with
<button ng-click(createTask) class="...">Create Task</button>

This is possibly because whenever the ng-click is called there are two calls made ,one by angular and other by ionic .As ionic and angular comes as a bundle when you are working with ionic project.
You could try one of this method.
1) have ionic and angular javascript seperated in your index.html
instead of including (ionic and angular in one javascript file)
ionic.bundle.min.js
use as
ionic.min.js
angular.min.js
2) Alternatively you can create a directive of your own instead of ng-click like below.
before that Include angular-touch.js and inject ngTouch as a module in your app in app.js
app.directive('myclick', function() {
return function(scope, element, attrs) {
element.bind('touchstart click', function(event) {
event.preventDefault();
event.stopPropagation();
scope.$apply(attrs['myclick']);
});
};
});
Hope this helps.

in your controller
$scope.createTask = function(task) {
alert(task.title);
};
add $scope.task= {}; to init

Related

Why use autocomplete with radio buttons?

I am going through the bootstrap 4 documentation.
What purpose does it do to set the autocomplete tag to off?
Is this something needed for bootstrap, or is it just good practice?
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="options" id="option1" autocomplete="off"
checked> Active
</label>
</div>
Unlike other browsers, Firefox by default persists the dynamic checked state of an <input> across page loads. Use the autocomplete attribute to control this feature.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio
The docs are confused on this. The link posted by #dontangg states autocomplete is used on radio buttons by Firefox, but the main doc page for the <input> element type explicitly says the opposite:
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-autocomplete
It probably doesn't hurt to include it but to me it doesn't make much sense for it to do anything on radio buttons.
It's about the browser stage.
<script type="text/javascript">
function fnPickedFlow(){
return #('input[name=pickedFlow]:checked').val();
}
$( document ).ready(function() {
console.log( "pickedFlow:" + fnPickedFlow() );
// The first time return "one"
// The second time return "two"
});
</script>
<input type="radio" name="pickedFlow" value="one" checked/>
<input type="radio" name="pickedFlow" value="two" />
The first time when page loads, Radio button one is selected by default. When changing the selection to two and submitting, the redirection happens. And user click browser back button (javascript:history.back()) to come back to this page, pickedFlow is pointing to one but the selected button in UI is two.
Change code add autocomplete off for each of radio button to avoid the situation:
<script type="text/javascript">
function fnPickedFlow(){
return #('input[name=pickedFlow]:checked').val();
}
$( document ).ready(function() {
console.log( "pickedFlow:" + fnPickedFlow() );
// Always return "one"
});
</script>
<input type="radio" name="pickedFlow" value="one" autocomplete="off" checked/>
<input type="radio" name="pickedFlow" value="two" autocomplete="off" />
Also see:
https://forum.vuejs.org/t/radio-button-retains-old-state-on-browser-back-button/97417
Radio inputs are not compatible with autocomplete. However, it doesn’t hurt anything to have it there. It’s up to you if you want to keep it.

ionic - tag with ng-click in another tag with ng-click

I have tag with ng-click in another tag with ng-click. when i click on the internal tag, its start the two function instead of only internal function. example:
<div ng-click="gotoChat()" >
<i class="icon" ><button ng-click="Close()" class="button button-assertive">סגור בקשה</button></i>
</div>
When I click on the button tag, its start gotoChat() and Close() instead of only Close(). How can I fix it?
Couple of Things:
1) You are using Ionic2/Angular2 so you should use (click) event and ion-button (for RC release)
<div (click)="gotoChat()">
<i class="icon"><button ion-button (click)="Close($event)" class="button button-assertive">Name</button>
</div>
2) What you are looking for is $event.stopPropagation(). Call it on your nested event to keep the event from bubbling up. So, like this:
Close($event) {
$event.stopPropagation();
// handle event
}
Make sure to pass the $event object to the function you wish to call stopPropagation() on

onclick not working on Android Emulator (ionic framework)

Steps:
1. ionic start Hello blank
Add button to the index.html
<ion-content>
<button class="button" onclick="javascript:alert('x');">Try Click</button>
</ion-content>
inside hello dir: ionic run android
click the 'Try Click' button. Nothing happens.
Info:
Emulator: Android 4.4.2 API 19
change it to ng-click and in general it is better to call a function.
You should separate your logic from your views.
example HTML
<div ng-controller="LoginController">
<ons-button ng-click="yourFunction()">Click Me!</ons-button>
</div>
JS
.controller('SomeController', function () {
$scope.yourFunction = function () {
console.log("x is triggered");
alert('x');
}
}
Use
<ion-content>
<button class="button" ng-click="javascript:alert('x');">Try Click</button>
</ion-content>
ng-click instead of onclick..

Avoid modal dismiss on enter keypress

I have set up a bootstrap modal with a form inside it, I just noticed that when I press the Enter key, the modal gets dismissed.
Is there a way not to dismiss it when pressing Enter?
I tried activating the modal with keyboard:false, but that only prevents dismissal with the ESC key.
I just had this problem too.
My problem was that i had a close button in my modal
<button class="close" data-dismiss="modal">×</button>
Pressing enter in the input field caused this button to be fired. I changed it to an anchor instead and it works as expected now (enter submits the form and does not close the modal).
<a class="close" data-dismiss="modal">×</a>
Without seeing your source, I can't confirm that your cause is the same though.
Just add the type="button" attribute to the button element, some browsers interpret the type as submit by default.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes
This applies for all the buttons you have in the modal.
<button type="button" class="close" data-dismiss="modal">×</button>
I had this problem even after removing ALL buttons from my Bootstrap Modal, so none of the solutions here helped me.
I found that a form with a single text field would cause the browser to do a form submit (and result in dismiss), if you hit Enter while keyboard focus is on the text field. This seems to be more of a browser/form issue than anything with Bootstrap.
My solution was to set the form's onsubmit attribute to onsubmit="return false"
This may be a problem if you are actually using the submit event, but I'm using JS frameworks that generate AJAX requests rather than doing a browser submit, so I prefer disabling submit entirely. (It also means I don't have to manually tweak every form element that might trigger a submit).
More info here: Bootstrap modal dialogs with a single text input field always dismiss on Enter key
I had same problem, and i solved it with
<form onsubmit="return false;">
but there is one more solution, you can add dummy invisible input, so your form would look like this:
<form role="form" method="post" action="submitform.php">
<input type="text" id="name" name="name" >
<input type="text" style="display: none;">
</form>
You can put the login button before the cancel button and this would solve the issue you are having as well.
<div class="modal-footer">
<button type="submit" class="btn primary">Login</button>
<button type="submit" class="btn" data-dismiss="modal">Cancel</button>
</div>
I had a similar experience just now and the way I solved it was instead of using a tag, I changed the tag to an tag with type="button". This seemed to solve the problem of pressing the "enter" key and dismissing the bootstrap modal.
I had this problem too and I solved it this way. I added onsubmit to form. I also wanted to be able to use enter key as a saving key so I added save_stuff() javascript to onsubmit. return false; is used to prevent the form submit.
<form onsubmit="save_stuff(); return false;">
...
</form>
<script>
function save_stuff(){
//Saving stuff
}
</script>

jQuery mobile show and hide don't seem to work on iPhone

I'm trying to use jQuery show and hide which seem to work ok in Safari, when I try it on the iPhone, it doesn't work at all. Here is my code;
<script type="text/javascript">
$("#showSearch").click(function () {
$("#searchform").show(1000);
});
</script>
It's a button that when clicked, will show the search form. The form doesn't show on iPhone. Also when I add $('#showSearch').remove(); in there, it doesn't get removed, even if I add a function as the second parameter of show() it still doesn't hide the button.
Thanks for your help.
jQuery Docs:
http://api.jquery.com/show/ (Shows)
http://api.jquery.com/hide/ (Hides)
http://api.jquery.com/remove/ (Deletes)
http://api.jquery.com/toggle/ (Show/Hide)
Live Example:
http://jsfiddle.net/qQnsj/1/
JS
$('#viewMeButton').click(function() {
$('#viewMe').toggle(); // used toggle instead of .show() or .hide()
});
$('#removeMeButton').click(function() {
$('#removeMe').remove();
});
HTML
<div data-role="page" id="home">
<div data-role="content">
<div id="viewMe">
Hello I'm in the div tag, can you see me?
</div>
<br />
<button id="viewMeButton">Show / Hide</button>
<br />
<div id="removeMe">
Click the button to remove me
</div>
<br />
<button id="removeMeButton">Remove Me</button>
</div>
</div>
I know this is an old question, but I recently had a similar problem. Hopefully this will help someone else if they find this question, too.
I was trying to hide a set of ui-block-* elements in a JQM grid. On the same grid cells, my media query for the iPhone was setting display: block !important. That was winning.
The !important directive was unnecessary in my case and when I removed it the calls to hide() and show() began working as expected.