How to access checked checkbox value in controller?
Here radio buttons array fill many radio buttons.
<label class="item item-input" ng-show="item.types !=''">
<div ng-repeat="type in item.types" style="margin-left:5px;">
<label class="item item-radio">
<input type="radio" name="group" ng-model="param.type">
<div class="item-content">
{{type.name}}
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
</div>
</label>
And Controller code
$scope.addItem = function(params) {
alert(params.type);
};
Given that you didn't post the rest of your controller code, I can't say for sure that this is the problem (or your only problem), but I suspect that changing ng-model to type and changing params.type to $scope.type in your addItem function would do the trick. Like so:
<label class="item item-input" ng-show="item.types !=''">
<div ng-repeat="type in item.types" style="margin-left:5px;">
<label class="item item-radio">
<input type="radio" name="group" ng-model="type">
<div class="item-content">
{{type.name}}
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
</div>
</label>
And in your controller:
$scope.addItem = function(params) {
alert($scope.type);
};
Related
I'm using angular to populate a form, which can be edited. But the edits are not being captured by the scope variables.
Here's my code:
HTML code:
<div class="list">
<label class="item item-input">
<span class="input-label">Name</span>
<span>{{name}}</span>
</label>
<label class="item item-input">
<span class="input-label">Locality</span>
<span>{{locality}}</span>
</label>
<label class="item item-input">
<span class="input-label">City</span>
<span>{{city}}</span>
</label>
<label class="item item-input">
<span class="input-label">Amenities</span>
<input type="text" placeholder="Amenities" ng-model="amenities">
</label>
<label class="item item-input">
<span class="input-label">Total Rooms</span>
<input type="text" placeholder="Total Rooms" ng-model="trooms">
</label>
</div>
<div class="text-center">
<button class="button button-positive" ng-click="hotelUpdate()">
Save Edits
</button>
</div>
hotelUpdate() :
$scope.hotelUpdate = function() {
var hotel1 = {
objectId: $state.params.hotelId,
amenities: $scope.amenities,
free_rooms: $scope.frooms,
total_rooms: $scope.trooms
}
Backendless.Data.of( "Hotels" ).save(hotel1)
.then(function(savedObject){
console.log(savedObject);
})
.catch(function(error){
console.log(error);
})
}
}
Is there something wrong with the code? On using binding previously the same way, it has worked fine.
Before the function cal,declare a scope variable $scope.hotel = {},
In HTML code,change ng-model like ng-model = "hotel.amenities",
In controller,change the code to
var hotel1 = {
objectId: $state.params.hotelId,
amenities: $scope.hotel.amenities,
free_rooms: $scope.hotel.frooms,
total_rooms: $scope.hotel.trooms
}
I want to select multiple radio options.New to angularjs.
Steps to produce the issue:
Select any country from the 1st select menu -> Choose Reason A
Select any country from the 2nd select menu -> Choose Reason C
here the above Reason A option disappears and only Reason C is selected.
i want to hold the two different reasons for different countries section.
here is the fiddle
http://jsfiddle.net/Lini7/knfsv64m/8/
var app = angular.module('myApp', []);
app.controller('myController', function ($scope) {
$scope.showReasonA = function showReasonA(checkNoTin) {
console.log("print the notin value from data",this.item.noTin);
if (!this.item.country) return false;
var selectedCountry = this.item.country.country.toLowerCase();
var fourCountries = ["cayman islands", "albania"]
var isIt = fourCountries.indexOf(selectedCountry) >= 0;
console.log("print match country val",isIt);
console.log("get checknotin value before assign",checkNoTin);
if (checkNoTin) this.item.noTin = true;
console.log("final checknotin val",checkNoTin);
return isIt;
}
});
and the view
<body>
<div ng-app="myApp">
<div ng-controller="myController">
<select name="country_0" ng-model="item.country" ng-options="" required="required" class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" style=""><option value="" selected="selected"></option><option label="AFGHANISTAN" value="object:835">AFGHANISTAN</option><option label="cayman islands" value="object:836">cayman islands</option><option label="ALBANIA" value="object:837">ALBANIA</option><option label="ALGERIA" value="object:838">ALGERIA</option><option label="AMERICAN SAMOA" value="object:839">AMERICAN SAMOA</option></select>
<div class="row">
<label class="checkbox">
<input type="checkbox" ng-model="item.noTin" ng-disabled="item.tin" ng-checked="showReasonA(true)" ng-required="item.noTin" name="checkboxNoTin"/>
</label><span translate="transactions.changeAddress.crs.yes.me_no_tin" style="margin-top: 10px" class="col-value"></span>
</div>
<div ng-if="item.noTin" class="row">
<div class="col padding-top-xs"><span translate="transactions.changeAddress.crs.select_reason" style="margin-bottom: 12px;" class="pol-value col-value"></span>
<div ng-if="item.country.tin == true" ng-init="item.reason = 0" class="row margin-bottom-medium">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="A" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason1"></span>
</label>
</div>
<div ng-if="!showReasonA()" class="row margin-bottom-medium">
<div class="col">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="B" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason2_title"></span>
<label class="item item-input input-margin">
<textarea ng-model="item.othersReason" ng-required="item.reason == 2" rows="5"></textarea>
</label>
</label>
</div>
</div>
<div ng-if="!showReasonA()" class="row margin-bottom-medium">
<div class="col">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="C" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason3"></span>
</label>
</div>
</div>
</div>
</div>
<select name="country_0" ng-model="item.country" ng-options="" required="required" class="ng-not-empty ng-dirty ng-valid-parse ng-valid ng-valid-required ng-touched" style=""><option value="" selected="selected"></option><option label="AFGHANISTAN" value="object:835">AFGHANISTAN</option><option label="cayman islands" value="object:836">cayman islands</option><option label="ALBANIA" value="object:837">ALBANIA</option><option label="ALGERIA" value="object:838">ALGERIA</option><option label="AMERICAN SAMOA" value="object:839">AMERICAN SAMOA</option></select>
<div class="row">
<label class="checkbox">
<input type="checkbox" ng-model="item.noTin" ng-disabled="item.tin" ng-checked="showReasonA(true)" ng-required="item.noTin" name="checkboxNoTin"/>
</label><span translate="transactions.changeAddress.crs.yes.me_no_tin" style="margin-top: 10px" class="col-value"></span>
</div>
<div ng-if="item.noTin" class="row">
<div class="col padding-top-xs"><span translate="transactions.changeAddress.crs.select_reason" style="margin-bottom: 12px;" class="pol-value col-value"></span>
<div ng-if="item.country.tin == true" ng-init="item.reason = 0" class="row margin-bottom-medium">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="A" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason1"></span>
</label>
</div>
<div ng-if="!showReasonA()" class="row margin-bottom-medium">
<div class="col">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="B" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason2_title"></span>
<label class="item item-input input-margin">
<textarea ng-model="item.othersReason" ng-required="item.reason == 2" rows="5"></textarea>
</label>
</label>
</div>
</div>
<div ng-if="!showReasonA()" class="row margin-bottom-medium">
<div class="col">
<label class="radio-button">
<input type="radio" name="reason" ng-model="item.reason" ng-value="C" required="required"/><span class="indicator"></span><span translate="transactions.changeAddress.crs.yes.reason3"></span>
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
To make some input as hidden you can use
<p ng-show="condition">I'm shown</p>
and this condition value should be initialized in the scope of your controller
$scope.condition=true/false
to hide an show input you can use ng-if. Its better to use ng-if for dynamic inputs . see details here
Now for your case you can use item.reason in the condition to show and hide your input .
<input type="radio" name="reason" ng-model="item.other" ng-value="C" required="required" ng-if="item.reason='object:836'" />
Finally got the solution
I just added the radio button name with index then it worked which will give different names for each group of buttons.
name='reason_{{$index}}'
I have a list of animals and there are 2 sets of radio buttons against each animal- extinct(y or n), habitat(land or water).
If user selects extinct as y for an animal then habitat radio buttons get disabled.
If user selects extinct as n, then habitat radio button will be enabled and user can select either land or water as habitat.
And on change of value of the radio button I call a function, where I am getting the selected value. Now the problem is for both radio buttons I get the same value ie (y or n). How to get proper values ie y or n for extinct and land or water for habitat.
here is the code.. i have modified the names.
HTML content
<fieldset>
<legend>List of Animals</legend>
<div class="row" ng-repeat=" animal in animals">
<label class="col-md-2 text-info">{{ animal }}:</label>
<label class="col-md-1 text-info"> Extinct</label>
<div class="col-md-1">
<input id="{{animal}}extinctY" type="radio" name="{{animal}}extinct" data- ng-model="value" value="{{animal}}|Y" ng-change="getExtinct(value1)"/>
</div>
<label class="col-md-1 text-info">Y</label>
<div class="col-md-1">
<input id="{{animal}}extinctN" type="radio" name="{{animal}}extinct" data-ng-model="value" value="{{animal}}|N" ng-change="getExtinct(value1)" required="required"/>
</div>
<label class="col-md-1 text-info">N</label>
<label class="col-md-1 text-info"> Habitat</label>
<div class="col-md-1">
<input id="{{animal}}habitatL" type="radio" name="{{animal}}habitat" ng-checked="{{animal}}checked" ng-model="value" value="{{animal}}|L" ng-disabled="{{animal}}disabled" ng-change="getHabitat(value)"/>
</div>
<label class="col-md-1 text-info">Land</label>
<div class="col-md-1">
<input id="{{animal}}habitatW" type="radio" name="{{animal}}habitat" ng-checked="{{animal}}checked" ng-model="value" value="{{animal}}|W" ng-disabled="{{animal}}disabled" ng-change="getHabitat(value)"/>
</div>
<label class="col-md-1 text-info">Water</label>
</div>
<br/>
</fieldset>
JS content
$scope.animals= ['Zebra', 'Dinosaur', 'Whale', 'Antilon'];
$scope.getExtinct= function getExtinct(extinct) {
alert(extinct); // Here it displays Zebra|N
};
$scope.getHabitat = function getHabitat(habitat) {
alert(habitat); // Here also it displays Zebra|N instead of Zebra|L
};
The easiest way to get around this is to make the animals. I created a plunkr to demonstrate. http://plnkr.co/edit/s45WEnCbRvBL2CRmjbut?p=preview
Then you can evaluate the ng-disabled, ng-value, etc. using the keys.
<fieldset ng-controller="AnimalCtrl">
<legend>List of Animals</legend>
<div class="row" ng-repeat=" animal in animals">
<label class="col-md-2 text-info">{{ animal.name }}:</label>
<label class="col-md-1 text-info"> Extinct</label>
<div class="col-md-1">
<input type="radio" name="{{animal.name}}extinct" ng-model="animal.extinct" value="true" />
<label class="col-md-1 text-info">Y</label>
</div>
<div class="col-md-1">
<input type="radio" name="{{animal.name}}extinct" ng-model="animal.extinct" value="false" required/>
<label class="col-md-1 text-info">N</label>
</div>
<label class="col-md-1 text-info"> Habitat</label>
<div class="col-md-1">
<input type="radio" name="{{animal}}habitat" ng-model="animal.habitat" value="land" ng-disabled="animal.extinct == 'true'" />
<label class="col-md-1 text-info">Land</label>
</div>
<div class="col-md-1">
<input type="radio" name="{{animal}}habitat" ng-model="animal.habitat" value="water" ng-disabled="animal.extinct == 'true'" />
<label class="col-md-1 text-info">Water</label>
</div>
</div>
</fieldset>
Last week I made a function that takes a true/false value out of a pair of buttons (one for true, one for false) and puts it into a $scope variable.
It worked perfectly.
$scope.trueOrFalse = function(truefalse) {
if (truefalse == false){
$scope.value = false;
document.getElementById("makeFalse").className = "button active";
document.getElementById("makeTrue").className = "button";
console.log($scope.isBusiness);
} else if(truefalse == true){
$scope.value = true;
document.getElementById("makeTrue").className = "button active";
document.getElementById("makeFalse").className = "button";
console.log($scope.value);
}
}
But now, I have basically the same exact thing in another page, and its only working once, on the first click, where, if the chosen value is false, I get a console.log that it is false, then nothing happen if clicked again, to change the value.
But if the value chosen is true, then when the next click is attempted, I start to get:
TypeError: fn is not a function
at $parseFunctionCall (ionic.bundle.js:21045);
at ionic.bundle.js:53458;
at Scope.$get.Scope.$eval (ionic.bundle.js:23100);
at Scope.$get.Scope.$apply (ionic.bundle.js:23199);
at HTMLAnchorElement.<anonymous> (ionic.bundle.js:53457);
at HTMLAnchorElement.eventHandler (ionic.bundle.js:11713);
at triggerMouseEvent (ionic.bundle.js:2863);
at tapClick (ionic.bundle.js:2852);
at HTMLDocument.tapMouseUp (ionic.bundle.js:2925);
What's going on here?
Here's the HTML
<ion-view view-title="Biz Post/Uploading">
<ion-content>
<!-- Begin Second Set of Tabs -->
<div class="button-bar">
<a class="button" id="item" data-ng-click="isDeal(false)">Item</a>
<a class="button" id="deal" data-ng-click="isDeal(true)">Deal</a>
</div>
<!-- End Second Set of Tabs -->
<!-- Section -->
<div class="list-inset">
<center><img src="http://placehold.it/350x150"></center>
</div>
<div class="list">
<div class="item item-divider">
Add thing
</div>
<label class="item item-input item-stacked-label">
<span class="input-label">thing</span>
<input type="text" placeholder="add location" ng-model="thing.thing2">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">thing</span>
<input type="text" placeholder="add price" ng-model="thing.thing3">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">thing</span>
<input type="text" placeholder="add item description" ng-model="item.thing">
</label>
<label class="item item-input">
<span class="input-label">Date</span>
<input type="date">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">thing</span>
<input type="text" placeholder="set limit" ng-model="thing.thing4">
</label>
<button class="button button-full button-balanced" data-ng-click="pushData()">
push data
</button>
</div>
</ion-content>
</ion-view>
With ng-class the code would look like this:
http://plnkr.co/edit/6sOzUmAcIzVjLEOL9Jva?p=preview
<div ng-controller="MainCtrl">
<button id="makeTrue" ng-class="{button :true, active: truefalse}" ng-click="truefalse = !truefalse">Make true</button>
<button id="makeFalse" ng-class="{button :true, active: !truefalse} " ng-click="truefalse = !truefalse">Make false</button>
</div>
In controller: $scope.truefalse = true;. If truefalse is true, then makeTrue has the active class (that seems a bit off, but it's the same in your code), otherwise makeFalse is active.
This way you're not playing with the DOM in your controller(which should be avoided).And your $scope is less cluttered.
For the ng-click you can define a function on $scope and call it instead of changing truefalse in an expression (but that's up to you)
I am using radio buttons in a popover to create a dropdown type filter for my app.
When I use an iPad the radio buttons go haywire! This works perfectly on every other device.
Best illustration is in this video
https://www.youtube.com/watch?v=Begkz4uS8hQ
Here is my code:
<script id="templates/popover.html" type="text/ng-template">
<ion-popover-view>
<ion-content>
<div class="item bar-header bar-positive">
<h2 class="title">Difficulty</h2>
</div>
<label class="item item-radio">
<input type="radio" ng-model="filter.difficulty" value="" name="group">
<div class="item-content">
All
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio">
<input type="radio" ng-model="filter.difficulty" value="Easy" name="group">
<div class="item-content">
Easy
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio">
<input type="radio" ng-model="filter.difficulty" value="Medium" name="group">
<div class="item-content">
Medium
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio">
<input type="radio" ng-model="filter.difficulty" value="Hard" name="group">
<div class="item-content">
Hard
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio">
<input type="radio" ng-model="filter.difficulty" value="Expert" name="group">
<div class="item-content">
Expert
</div>
<i class="radio-icon ion-checkmark"></i>
</label>
</ion-content>
</ion-popover-view>
</script>
And displayed using this:
<button class="button button-positive button-small col col-20 icon-left ion-connection-bars filter-button" ng-click="popover.show($event)">
{{filter.difficulty || 'All'}}
</button>
Many thanks to anyone who can help.