Angular: radiobuttons stop firing "ng-change" after each one was clicked - javascript

I am building radio buttons dynamically. ng-change='newValue(value) stops being called after each radio button has been pressed once.
this works: Clicking on the radio buttons changes the value to foo/bar/baz.
http://jsfiddle.net/ZPcSe/19/
<div ng-controller="MyCtrl">
<input type="radio" ng-model="value" value="foo" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="bar" ng-change='newValue(value)'>
<input type="radio" ng-model="value" value="baz" ng-change='newValue(value)'>
<hr>
{{value}}
</div>
this code does not: The {{value}} - "label" is not updated once each radio button has been pressed at least once. Aparently ng-change is not fired any more.
<div ng-controller="MyCtrl">
<span ng-repeat="i in [0, 1, 2]">
<input name="asdf" type="radio" ng-model="value" value={{i}} ng-change='newValue(value)'>
</span>
{{value}}
</div>
http://jsfiddle.net/ZPcSe/18/
The Controlles is the same each time:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.value = '-';
$scope.newValue = function(value) {
$scope.value = value;
}
}
Thanks for your help.

ngRepeat creates new scope, so trying to set value sets it on the new scope. The workaround is to reference a property on an object that is on the parent scope--the object lookup happens on the parent scope, and then changing the property works as expected:
HTML:
<div ng-controller="MyCtrl">
<span ng-repeat="i in [0, 1, 2]">
<input name="asdf" ng-model="options.value" value="{{i}}" type="radio">
</span>
{{options.value}}
JS:
var myApp = angular.module('myApp', []);
function MyCtrl($scope) {
$scope.options = {
value: '-'
};
$scope.newValue = function(value) {
// $scope.options.value = value; // not needed, unless you want to do more work on a change
}
}​
You can check out a working fiddle of this workaround. See angular/angular.js#1100 on GitHub for more information.

Just a quick work-around, we can achieve the same- using ng-model="$parent.value", because it would refer to the parent of ng-repeat scope i.e- in myCtrl scope
Only Change in ng-model-
<input name="asdf" type="radio" ng-model="$parent.value" value={{i}} ng-change='newValue(value)'>
Here is the fiddle

Try ng-click instead of ng-change.

Related

Need to get value of the radio button and pass it to the backend using HTML and angularJS,and the data displayed in the frontend is a list

//This is my HTML code wherein am returning a list from backend.
<ul>
<li ng-repeat=" opt in bcfList1 track by $index" > <input type="radio" name="buildid" id="buildid" ng-model = $parent.selected ng-value="bcfList1" required>
{{ opt }}
</li>
</ul>
//This is my controller.js program
$scope.getDetails =function(data){
var id=data.id;
$('#addNode3').modal('show');
UpgradeService.getDataById(id).then(function(data){
if(data!=null){
$scope.List1=data.BUILDNUMBER;
}
});
}
I need to get the string value that'll be listed in front of the radio button. So once I click on radio button it should send that value to the controller.js
By using ng-model I need a solution.
Help me out!!
You need to add ng-change to your input fields with a call to that function. Here is a quick demo:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.b = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$scope.getDetails = function(index) {
console.log("sending data", index,$scope.selected);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="a in b track by $index">
<input type="radio" ng-model="$parent.selected" ng-value="a" ng-change="getDetails($index)" /> {{a}}
</div>
</div>
If I understand correctly, you need to gather which input type radio was clicked in controller and send this information to backend.
ng-model directive is very good approach here and you can use it just like so:
html
<label>
One
<input type="radio" value="one" ng-model="radio">
</label>
<label>
Two
<input type="radio" value="two" ng-model="radio">
</label>
<br><br>{{ radio }}<br>
JS
app.controller('MainCtrl', function($scope) {
$scope.radio = '';
$scope.consoleLogRadio = function() {
console.log($scope.radio);
}
});
Take a look at plunker example

how to assign and remove values of ng-model using checkbox?

<label><input type="checkbox" value="{{item}}" data-ng-model="untilcancelled" ng-init="untilcancelled=true"/>Until Cancelled</label>
<input type="text" ng-if="!untilcancelled" sipenddate data-ng-model="item.SIPTo" readonly/>
I have a checkbox which on checked I have to assign default value to item.SIPTo. Otherwise if unchecked I want to enable entering different to item.SIPTo
.The given textbox is a Datepicker
Use ng-change on check box, check the value of 'untilcancelled'. Then update your 'item.SIPTo'
<label><input type="checkbox" ng-change="updateItem()" value="{{item}}" data-ng-model="untilcancelled" ng-init="untilcancelled=true"/>Until Cancelled</label>
//controller function
$scope.updateItem = function() {
if($scope.untilcancelled) {
// check here
}
}
to toggle (true/false only)
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.MyScopeModel = {
Selected: true,
};
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.2/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<input type="checkbox" id="chkToggleId" name="chkToggleName" ng-model="MyScopeModel.Selected" />
<label ng-if="MyScopeModel.Selected" for="chkToggleId">
I am selected
</label>
<label ng-if="!MyScopeModel.Selected" for="chkToggleId">
I am not selected
</label>
<br>
<br>Check box value : {{MyScopeModel.Selected}}
</div>

I want to use radio button with ng-repeat but it's not work

This is AngularJS I write.
<script type="text/javascript">
var app = angular.module("App1", []);
app.controller("ctrl1", function ($scope, $filter) {
$scope.GenderChoice = ["Male","Female"];
});
</script>
This code below is work. "Gender" changes value when i change radio button.
<input id="MaleOption" name="oo" type="radio" ng-model="Gender" value="{{GenderChoice[0]}}" />
<input id="FemaleOption" name="oo" type="radio" ng-model="Gender" value="{{GenderChoice[1]}}" />
I want to use ng-repeat with radio button. This code below is not work. When i click change radio button, "Gender" is not response. Actually, "Gender" never shows the value.
<input ng-repeat="x in GenderChoice" id="{{x}}Option" name="oo" type="radio" ng-model="Gender" value="{{x}}" />
This code below is not work too.
<input ng-repeat="x in GenderChoice" id="{{x}}Option" name="oo" type="radio" ng-model="Gender" value="{{GenderChoice[$index]}}" />
What wrong with these code. Thank for advance.
ng-repeat creates a new scope for each iteration. Due to the way that JavaScript Prototype Inheritance affects Angular, and the fact that you are binding to a primitive rather than an object, you actually have two separate Gender properties, and the radio buttons are not interconnected.
The preferred way to solve this would be to bind to a property on an object (the "Always use a dot in your bindings" rule), which will automatically imply the same object for both buttons. i.e.
app.controller("ctrl1", function ($scope, $filter) {
$scope.GenderChoice = ["Male","Female"];
$scope.selected = {};
});
<input ng-repeat="x in GenderChoice"
id="{{x}}Option" name="oo" type="radio"
ng-model="selected.Gender" value="{{x}}" />
look at this pice of code it's all what you need
<style>
.green {
color: green;
}
.red {
color: red;
}
</style>
<input ng-repeat="x in GenderChoice"
id="{{x}}Option" name="oo" type="radio" ng-model="Gender" value="{{x}}" ng-click="class(Gender)"/>
<p class={{append}}>{{gender}}</p>
you need to submit Gender to access its value
var app = angular.module("App1", []);
app.controller("ctrl1", function ($scope, $filter) {
$scope.GenderChoice = ["Male","Female"];
$scope.class = function(x){
if (x == 'Male') {
$scope.append = 'green';
$scope.gender = 'Male';
}
if (x == 'Female') {
$scope.append = 'red';
$scope.gender = 'Female';
}
}
});
working plunker

Argument '' is not a function, got undefined in Kendo ( AngularJS )

I want to make a Kendo grid with 4 tabstrips, 4 children grids, 5 controllers, first is parent, others are children. Here is a part of code, with one parent and one child controller. Problem is that all the time I got an error "Argument '' is not a function, got undefined" Where should I define it? Everything is stored locally so the preview is not possible
Check this out:
http://fdietz.github.io/recipes-with-angular-js/controllers/sharing-models-between-nested-controllers.html
You don't nest the controllers in your javascript. This is from that link:
var app = angular.module("MyApp", []);
app.controller("MyCtrl", function($scope) {
$scope.name = "Peter";
$scope.user = {
name: "Parker"
};
});
app.controller("MyNestedCtrl", function($scope) {
});
Instead, you nest the controllers in your markup. I don't see where you are binding the controllers in your markup, btw.
<body ng-app="MyApp">
<div ng-controller="MyCtrl">
<label>Primitive</label>
<input type="text" ng-model="name">
<label>Object</label>
<input type="text" ng-model="user.name">
<div class="nested" ng-controller="MyNestedCtrl">
<label>Primitive</label>
<input type="text" ng-model="name">
<label>Primitive with explicit $parent reference</label>
<input type="text" ng-model="$parent.name">
<label>Object</label>
<input type="text" ng-model="user.name">
</div>
</div>
</body>
This is all from that link I provided.

Change radio model on button click [angularjs]

What i want to achieve is trigger the change of the model "value" of the radio when i click on the button change input.
Why do i need to call twice click so that the model gets updated and the radio gets checked?
http://jsfiddle.net/7GfB9/
HTML
<div ng-app ng-controller="Ctrl">
<input type="radio" ng-model="value" ng-value="33">
<input type="radio" ng-model="value" ng-value="22">{{ value }}
<button onclick="changeInput()">Change input</button>
</div>
JS
function Ctrl($scope) {
}
function changeInput() {
$('input:eq(0)').click().click();
}
Likely because the value is updated before your onclick function is called. Why don't you update the value in your onclick function and only use one click?
Forget jQuery and try this :
<button ng-click="value = 33">Change input</button>
http://jsfiddle.net/7GfB9/3/
Solution i've come up with. I'm new to angularjs seems like im not used to it yet.
HTML
<div ng-app ng-controller="Ctrl">
<input type="radio" ng-model="value" ng-value="33">
<input type="radio" ng-model="value" ng-value="22">{{ value }}
<button ng-click="changeInput()">Change input</button>
</div>
js
function Ctrl($scope) {
$scope.changeInput = function () {
$scope.value=$('input:eq(0)').val();
}
}

Categories