Angular radio button listing with ng-repeat - angularjs

How can i clear radio buttons checked status when click on 'clear' buttons ?
Listing radio buttons in ng-repeat,
<label ng-repeat="item in items">
<input type="radio" name="test" ng-model="test" value="{{item.name}}" />{{item.name}}
</label>
<a class="undo" ng-click="clear()">Clear</a>
fiddle
And is it possible clear checked status from another controller ?

This will works fine..!!!
angular.module("myApp", []).controller("Example", ["$scope", function($scope) {
$scope.data = {};
$scope.items = [
{name: 'one'},
{name: 'two'},
{name: 'three'},
{name: 'four'},
{name: 'five'},
];
$scope.clear = function(){
$scope.data = {};
//alert("hello");
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
<div ng-app="myApp" ng-controller="Example">
<label ng-repeat="item in items">
<input type="radio" name="test" ng-model="data.test" value="{{item.name}}" />{{item.name}}
</label>
<br>
<a class="undo" ng-click="clear()">Clear</a>
</div>

You can use a broadcast event. See this answer: https://stackoverflow.com/a/19498009/4161992
Then in your controller that controls your input radio, do this:
.controller('radioController', function($scope){
$scope.$on('triggerClearRadio', function(){
$scope.items.forEach(function (item, i) {
item.checked = false;
});
});
});
Change your input html to use ng-model="item.checked" instead of ng-model="test"
From your clear button you can broadcast the 'triggerClearRadio' event (name it something better though) like this:
<button type="button" ng-click="$root.$broadcast('triggerClearRadio')>Clear radios</button>
See this fiddle: http://jsfiddle.net/takuan/aaoub2mg/

Related

Passing checkbox values to controller in Angular JS

I have a group of checkboxes which are populated from,the database. Each checkbox value contains a Book Id.
Now I have a Save button which contains a ng-click="save()" function. How can I pass the value of checked book Ids to the controller, and how can I catch the array of Ids in the controller' save() method. How can I bind the checkbox values, with the controller, here?
Here is the code in the view,
<div ng-repeat="bk in Books">
<input type="checkbox" value={{bk.Id}} ng-checked="$index ===0 ? true : false"/>{{bk.Name}}
</div>
<br/>
<input type="button" id="save" class="btn btn-success" value="Save" ng-click="save()"/>
<br/>
You don't need to pass the Ids values from the view, you can grab them directly within the controller through the binding mechanism.
Here you have a working sample. Whit this you can get rid of value={{bk.Id}} and ng-checked="$index ===0 ? true : false". Also notice how it is initialized the state (checked or unchecked) of the checkbox: ng-init="bookModel[bk.Id] = $first" which sets the checkbox checked only for the first element in the list.
angular
.module('app', [])
.controller('ctrl', function($scope) {
$scope.Books = [{
Id: 1,
Name: "Harry Potrer"
},
{
Id: 2,
Name: "Python"
},
{
Id: 3,
Name: "C#"
}
];
$scope.bookModel = {};
$scope.save = function() {
var checkedBooks = [];
for (var k in $scope.bookModel) {
if ($scope.bookModel.hasOwnProperty(k) && $scope.bookModel[k]) {
checkedBooks.push(k);
}
}
//do your stuff with the ids in `checkedBooks`
alert(checkedBooks);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<span ng-app="app" ng-controller="ctrl">
<div ng-repeat="bk in Books">
<input type="checkbox" ng-model="bookModel[bk.Id]" ng-init="bookModel[bk.Id] = $first"/>{{bk.Name}}
</div><br/>
<input type="button" id="save" class="btn btn-success" value="Save" ng-click="save()" />
</span>

angularjs: forEach in ng-click

I have a problem with implementing "select All" feature for simple list:
Controller:
$scope.numbers = [{v:1},{v:2},{v:3},{v:4}];
Template:
select All
<p ng-repeat='n in numbers'>
<label>
<input type="checkbox" ng-model="n.selected">
Label: {{ n.v }}
</label>
</p>
It seems angular parser cannot process the ng-click statement:
Error: [$parse:syntax]
(...)
at relational (angular.min.js:234)
at r.equality (angular.min.js:233)
at r.logicalAND (angular.min.js:233) "<a href="" ng-click="numbers.forEach(function(v) {v.selected=true;});">"
Plunker demo:
https://plnkr.co/edit/6OZP02SZ5ZYa0iO4PBY3?p=preview
Can I implement that just in the html template or have to use the controller method?
Edit: Controller method works fine, I'm just still interested why in-place code doesn't parse though?
Try like below, numbers.forEach(function(v) {v.selected=true;}); is wrong will throw the error because numbers doesn't have forEach menthod and anonymous function will not work in ng-click
This is the best way to perform select all
<div ng-app="myApp" ng-controller="myCtrl">
<label>
<input type="checkbox" ng-model="all">
Select All
</label>
<p ng-repeat='n in numbers'>
<label>
<input type="checkbox" ng-checked="all">
Label: {{ n.v }}
</label>
</p>
</div>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.numbers = [{v:1},{v:2},{v:3},{v:4}];
});
https://fiddle.jshell.net/mspj0z65/1/
HTML
<div ng-app="myApp" ng-controller="myCtrl">
select All
<p ng-repeat='n in numbers'>
<label>
<input type="checkbox" ng-model="n.selected">
Label: {{ n.v }}
</label>
</p>
</div>
Angular script:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.numbers = [{v:1},{v:2},{v:3},{v:4}];
$scope.loop = function(){
angular.forEach(numbers, function(v) {
v.selected=true;
});
}
});
https://fiddle.jshell.net/mspj0z65/
It is better implements the method on controller as:
$scope.selectAll=function() {
$scope.numbers.forEach(function(v) {
v.selected=true
});
}
and in view use ng-click="selecAll()"
Let your view as simple as posible and make the code more readable ans ease to
understand by third parties or own in the future.
I recomend take a view to John Papa Angular style guide

ng-dialog: input radio button value is undefined

I'm trying to get the radio value button value from ng-dialog but it always got undefined.
Here is the dialog template:
<script type="text/ng-template" id="flag-reasons.html">
<div style="padding:10px;">
<span>
What's wrong ?
</span>
<div class="form-group" ng-repeat="flagreason in flagreasons">
<input type="radio" ng-model="fr" name="frname" value="{{flagreason.id}}"> {{flagreason.title}}
</div>
<div class="form-group">
<button type="button" ng-click="validFlag()">
Valider
</button>
</div>
</div>
</script>
Here is the partial js where I start with the dialog:
$scope.openFlag = function(){
$scope.dialog = ngDialog.open({ template: 'flag-reasons.html',
className: 'ngdialog-theme-default', scope: $scope });
$scope.validFlag = function(){
console.log($scope.fr);
}
}
I have tried ng-value as below:
<input type="radio" ng-model="fr" name="frname" ng-value="flagreason.id"> {{flagreason.title}}
but it still got undefined
Notice that it works when I directly set the value of the radio button like:
<input type="radio" ng-model="fr" name="frname" value="5"> {{flagreason.title}}
The issue is with ngmodel that's not getting update. You have to initialise ngmodel first in the template.
flag-reasons.html
<script type="text/ng-template" id="flag-reasons.html">
<div style="padding:10px;">
<span>
What's wrong ?
</span>
<div class="form-group" ng-repeat="flagreason in flagreasons">
<input type="radio" ng-model="cb.fr" name="frname" value="{{flagreason.id}}">{{flagreason.id}} - {{flagreason.title}}
</div>
<div class="form-group">
<button type="button" ng-click="validFlag()">
Valider
</button>
</div>
</div>
</script>
Controller
angular.module("app", ['ngDialog'])
.controller('Ctrl',function ($scope, ngDialog) {
'use strict';
$scope.cb = {};
$scope.flagreasons = [
{id: 1, title: 'title1'},
{id: 2, title: 'title2'},
{id: 3, title: 'title3'}
];
$scope.openFlag = function(){
$scope.dialog = ngDialog.open({ template: 'flag-reasons.html',
className: 'ngdialog-theme-default', scope: $scope });
$scope.validFlag = function(){
console.log($scope.fr);
}
}
$scope.$watch('cb.fr', function (v) {
console.log(v);
});
});
Working Fiddle: JSFiddle

How to bind one angular model to the value of another ng-model

I have a radio button being selected like this:
<input type="radio" ng-model="lesson.sectionID" value="{{section.$id}}">
I want to bind the value of that input to another model, I tried the following:
<input type="text" ng-model="module.sectionID" ng-bind="lesson.sectionID">
and
<input type="text" ng-model="module.sectionID" ng-value="lesson.sectionID">
When I tried ng-value it set the text input to the correct value but the actual value of the model was not being set.
You can assign your model using
ng-model="module.sectionID"
ng-value="module.sectionID=lesson.sectionID"
You probably looking for this solution
angular.module('choices', [])
.controller("MainCtrl", ['$scope', function($scope) {
$scope.color = '';
$scope.colors = [
"Red",
"Green",
"Blue",
""
];
$scope.changeColor = function(){
$scope.color = "Red"
};
}]);
<html>
<head>
<body ng-app="choices" ng-controller="MainCtrl">
<div ng-repeat="color in colors">
<input type="radio" ng-model="$parent.color" ng-value="color" id="{{color}}" name="color">
<label >
{{color || 'Other'}}
</label>
<input type="text" ng-model="$parent.color" ng-show="color==''">
</div>
<p></p>
The chosen color is <strong>{{color}}</strong>
<p></p>
<button ng-click="changeColor()">Change color</button>
</body>
</html>

Angular JS binding scope data within ng-repeat to form

I have a collection of items in $scope.data with fields "id","name" & "age", that are being displayed in the view using ng-repeat directive.
For each set of items, there is a corresponding "edit button".
I want to be able to access values for the particular set of items for which edit button was pressed.
Html:
<div ng-controller="Ctrl">
<div ng-repeat="i in data">
Name: {{i.name}}
Age: {{i.age}}
<form ng-submit="submit()">
<input type="text" ng-model="i.id"/>
<input type="submit" value="Edit" />
</form>
</div>
</div>
Script:
function Ctrl($scope)
{
$scope.data = [
{id:1,name:"Alex",age:22},
{id:2,name:"Sam", age:28}
];
$scope.submit = function() {
//access id of user for which edit was clicked
};
}
What is the right way to do this?
Instead of a form, I would suggest you just use a button:
<div ng-controller="Ctrl">
<div ng-repeat="i in data">
Name: {{i.name}}
Age: {{i.age}}
<input type="text" ng-model="i.id"/>
<button ng-click="submit(i)">Edit</button>
</div>
</div>
Just send i into the button click event (I suppose you could use form if you need validation, but your example doesn't seem to need it).
Your submit function then changes to:
$scope.submit = function(selectedItem) {
// here you now have access to selected item
};
Try this:
HTML:
<form ng-submit="submit(i.id)">
JavaScript:
$scope.submit = function(userId) {
// you have userId
};
One option is to just use an ng-click within a button that calls your submit passing in i
<div ng-controller="Ctrl">
<div ng-repeat="i in data">
Name: {{i.name}}
Age: {{i.age}}
<button ng-click="submit(i);">edit</button>
</div>
</div>
and the function:
$scope.submit = function(i) {
console.log(i);
//access id of user for which edit was clicked
};
Here's a fiddle of that working: http://jsfiddle.net/pRAcP/

Resources