Button inside the label with the input does not trigger ng-click - angularjs

That's my code:
<label class="item item-input " style="height: 10%" ng-click="publish()">
<input type="text" placeholder="Title" ng-model="title">
<i ng-click="publishBlog()" class="button button-clear icon ion-paper-airplane padding-right"></i>
</label>
The "publish()" can be triggered,but not the "publishBlog()".The ionic will put the icon into the .Does the ionic cause it?

within "label" tag your click event will not work, because label is overriding your button click.
so, use DIV instead of label , that will work fine.

As Hardy said, you will have to use div instead of label. But you will be required to use $event.stopPropagation(); to make sure only publishBlog() gets called. Without $event.stopPropagation();, both the functions will get called.
So, here's a sample implementation:
<ion-content class="padding" ng-controller="my">
<div class="item item-input " style="height: 10%" ng-click="publish($event)">
<input type="text" placeholder="Title" ng-model="title">
<i ng-click="publishBlog($event)" class="button button-clear icon ion-paper-airplane padding-right"></i>
</div>
</ion-content>
And your controller:
.controller("my", function($scope){
$scope.publish = function($event) {
alert("title");
};
$scope.publishBlog = function ($event) {
$event.stopPropagation();
alert("icon");
};
});
Here's a demo: http://play.ionic.io/app/1b82ce25ca44

Related

How to get the values that are in ng-model, which comes from a select in angularjs1?

I created a select, where I get a person and their values. But when I try to pass these values on ng-model this is giving undefined.
in html.
<ion-view view-title="Cadastro Livro Preto">
<ion-nav-buttons side="left">
</ion-nav-buttons>
<ion-content>
<div class="list card">
<select ng-model="cadLivroPreto" ng-options="y.nome for (x, y) in linkPessoasLoja">
</select>
<label class="item item-input item-stacked-label">
<h3 ng-model=cadLivroPreto.nome>Nome: {{cadLivroPreto.nome}}</h3><br/>
</label>
<label class="item item-input item-stacked-label">
<h3 ng-model="cadLivroPreto.cpf">CPF: {{cadLivroPreto.cpf}}</h3><br/>
</label>
</div>
<h3>teste: {{cadLivroPreto.id}}</h3><br/>
<h3>teste: {{cadLivroPreto.nome}}</h3><br/>
<!--
Acrescentar telefone
-->
</div>
<button class = "button button-block button-dark" ng-click="Cadastrar()"> Cadastrar</button>
</ion-content>
</ion-view>
in my controller.js
$scope.cadLivroPreto = {};
//$scope.pessoa = {};
$scope.Cadastrar = function(){
console.log($scope.cadLivroPreto.nome);
console.log($scope.cadLivroPreto.cpf);
};
In the html what is in the expression {{}} I can see on the screen, but when I try to give console.log the same putting it in the ng-model in the controller is appearing undefined.
Can anybody help me ? I do not know what I'm doing wrong.

How to give a specific value to input with ' ng-model="searchQuery" '?

I am creating an app using Ionic Framework and I'm facing a problem.
I'd like to create a search function with tags. When we click on a button, the value is given to an input.
My code :
<div id="rech-btns" class="padding" ng-model="searchQuery" ng-controller="RechCtrl">
<button class="button button-outline button-balanced espace rech-text" ng-click="showDiv('Facile')">
Facile
</button>
<!-- There are others buttons but I deleted them for you to see the whole code -->
</div>
<div id="rechrech">
<label class="item item-input">
<i class="icon ion-search placeholder-icon"></i>
<!-- <input type="search" placeholder="Recherche..." /> -->
<input type="search" ng-model="searchQuery">
<!--<input type="text" text="facile" ng-model="searchQuery">-->
</label>
<div class="padding">
<button id="paramrech" class="button button-outline button-balanced espace rech-text">
<!-- the value arrives here -->
</button>
</div>
I tried to change the value with my function but it doesn't work so I'm asking you :p
My function :
.controller('RechCtrl', function($scope) {
$scope.showDiv = function(param) {
document.getElementById('rech-btns').style.display = "none";
//so when we click, this disappear
document.getElementById('rechrech').style.display = "block";
//this appear
document.getElementById("paramrech").innerHTML = param;
//and here I don't know what to do to change what I want
};
})
Do you have any idea ?

Value of input tag not updating from a list in AngularJS+Ionic

I am trying to set the value of input equal to the item in ion-list based on a click event. The value updates on first click event but is not updating on later clicks
<ion-view view-title="Add Cities">
<ion-content>
<label class="item-input-wrapper bar-subheader">
<i class="icon ion-android-search placeholder-icon"></i>
<input style="width:100%" type="text" placeholder="Search" ng-model="CityName">
</label>
<ion-button class="button ion-plus button-positive" ng-click="addCity(CityName)" >Add</ion-button>
<div class="list">
<ion-list>
<ion-item ng-repeat="item in states|filter:CityName" ng-click="set(item)">
{{item}}
</ion-item>
</ion-list>
</ul>
</div>
</ion-content>
</ion-view>
associated function in controller.js
$scope.set=function(title){
$scope.CityName=title;
console.log(title);
}
i have checked console the function triggers but the input updates only once
i have tried adding $scope.$apply() in function set but it's still not working. Please suggest edits in the above code or is there a better method to do the same?
The answer is explained here by #Pankaj Parkar
My problem was fixed by renaming ng-model="CityName" to ng-model="data.CityName"
<input style="width:100%" type="text" placeholder="Search" ng-model="data.CityName">
and changing this
$scope.set=function(title){
$scope.CityName=title;
console.log(title);
}
to
$scope.set=function(title){
$scope.data={'CityName':title};
console.log(title);
}

Form is not accessible inside controller

I have this ionic page:
<ion-view view-title="Sign up for business">
<ion-nav-bar class="bar-positive">
<ion-nav-back-button></ion-nav-back-button>
<ion-nav-buttons side="right">
<button class="button button-icon icon ion-android-done" ng-disabled="bizzSForm.$invalid" ng-click="submitBizzForm()"></button>
</ion-nav-buttons>
</ion-nav-bar>
<ion-content>
<form name="bizzSForm" class="css-form" ng-submit="submitSignUpForm()" novalidate>
<div class="list">
<label class="item item-input">
<i class="icon ion-person placeholder-icon"></i>
<input type="text" placeholder="Your business or organization name" name="name" ng-model="bizzFormData.name" ng-minlength="3" ng-maxlength="50" required>
</label>
<div ng-show="bizzSForm.$submitted || bizzSForm.name.$touched">
<span ng-show="bizzSForm.name.$error.required" class="form-err-blk"><i class="ion-close-round"></i> Tell us your business or organization name.</span>
</div>
I have this controller code:
$scope.submitBizzForm = function() {
if($scope.bizzSForm.$valid) {
$scope.submitSignUpForm();
}
}
here $scope.bizzSForm is showing undefined
You can pass the bizzSForm in the ngSubmit callback, and use it as it is:
HTML
<form name="bizzSForm" class="css-form" ng-submit="submitSignUpForm(bizzSForm)" novalidate>
<!-- content -->
</form>
Javascript
$scope.submitBizzForm = function(form) {
if(form.$valid) {
$scope.submitSignUpForm();
}
};
You need to pass form name as a parameter to the function you want ngSubmit to handle.
<form name="bizzSForm" class="css-form" ng-submit="submitSignUpForm(bizzSForm)" novalidate>
You see that, form should have a name, name="bizzSForm" and pass into function, ng-submit="submitSignUpForm(bizzSForm)"
then inside controller, you just need to call it,
$scope.submitSignUpForm = function(bizzSForm) {
if(bizzSForm.$valid) {
// do something here
console.log('Form is valid');
}
}

Strange Ionic Framework behavior with ng-repeat

I have an ng-repeat with a button inside which calls a controller method.
I'm using the Ionic Framework which is based on AngularJS
I can't get the button to actually trigger the controller method unless I revert to using plain Angular (or one more variant, which I will show later).
My example of the broken situation can be seen here in jsfiddle.
You can see that the red button works (generates an alert) and the green, inside the ng-repeat doesn't, while it is calling the exact same controller function.
Here is the controller code:
angular.module('todoApp', [])
.controller('TodoController', ['$scope', function($scope) {
var answers = [];
addOptionToArray("Option 1");
addOptionToArray("Option 2");
addOptionToArray("Option 3");
$scope.answers = answers;
$scope.doSomething = function() {
alert("clicked");
};
function addOptionToArray(optionText) {
answers.push({text : optionText});
}
}]);
and the page layout:
<div ng-app="todoApp">
<div ng-controller="TodoController">
<button class="button button-assertive" ng-click="doSomething()">
<i class="icon ion-minus-circled"></i>
</button>
<label class="item item-input item-button-right" ng-repeat="answer in answers">
<input type="text" ng-model="answer.text">
<div class="buttons">
<button class="button button-balanced" ng-click="doSomething()">
<div>click</div>
</button>
</div>
</label>
</div>
</div>
Now there are two thins that will make the button inside the ng-repeat work again
Replace the Ionice bundle JS reference with a pure `AngularJS' one as can be seen here in jsfiddle
While keeping the Ionic JS reference, remove the <input>' inside the ng-repeat see jsfiddle
Any idea of why having the <input> inside the ng-repeat breaks the button's ng-click?
Or what is broken in Ionic?
Your problem is with block elements inside inline elements. Don't put <div> inside <button> or <label>. Fix it and it works as expected:
<div ng-app="todoApp">
<div ng-controller="TodoController">
<i class="icon ion-minus-circled"></i>
<div class="item item-input item-button-right" ng-repeat="answer in answers">
<input type="text" ng-model="answer.text" />
<div class="buttons">
Click
</div>
</div>
</div>
</div>

Resources