The ng-change functionality of Angular radio buttons only functions correctly the first time it

I am struggling to comprehend why the angular ng-change function is only being called on the first click. Take a look at this example:

http://jsfiddle.net/ZPcSe/5/

The function in the provided example is triggered every time the radio selection is changed, however in my code something seems to be amiss.

http://jsfiddle.net/4jL3u8ko/1/

Could someone assist me in modifying my code to mimic the behavior of the first example and also explain why it's currently not working?

Answer №1

Interesting inquiry.

While not a direct answer, this can be seen as explanations to potential issues and solutions that may arise. This scenario is common, so it's crucial to grasp. The response from @Victor is satisfactory and the input from @Atias is also valuable.

Breakdown -

Basic breakdown : ng-repeat creates child scopes.

More detailed explanation :

In your application, there are 3 scopes (potentially more based on $scope.radioButtons values). Let's name them - parentScope (main scope), childScope1 (first element of ng-repeat), and childScope2 (second element of ng-repeat).

Main scope variables: calledFunctions (array), radioButtons (object), and newValue (function).

ChildScope1 variables: name (initially assigned a literal), val (initially assigned an object), and value (currently undefined).

ChildScope2 variables: name (initially assigned a literal), val (initially assigned an object), and value (currently undefined).

What occurs when you click the radio button in childScope1 (first time):

ChildScope1.value = ChildScope1.name or ChildScope1.value= "Radio1"; due to ng-change directive detecting a model change (from undefined to a literal "Radio1"), triggering ChildScope1.newValue() which doesn't exist, leading to a call to parentScope.newValue().

Note: ChildScope1.value= "Radio1";

After clicking other radio buttons......

If you click ChildScope1's radio button again: no change in value means no function calls beyond this point.

The same process applies to ChildScope2.

This explains the one-time binding behavior in your fiddle, echoing insights from @Victor and @Atias.

Solution: Ensure ng-model in ChildScope1, ChildScope2, or any child scopes points to a parent scope variable using $parent or creating an object with properties in the parent scope (e.g., ParentVal={ChildVal = ""}), written in ng-model inside ng-repeat as ng-model=ParentVal.ChildVal.

Apologies for any language errors; thank you for your understanding.

Gratitude

Answer №2

ng-model can be quite tricky to work with. It is always important to ensure that there is a dot in your models to avoid creating multiple unintended models.

A simple workaround is to use $parent.value instead of just value in your ng-model and ng-change.

To resolve this issue properly, it is recommended to have a structured model object within the scope rather than storing values directly:

$scope.model = {};

ng-model="model.radiosValue"

For an even more effective solution, consider adopting the controllerAs pattern and utilize that object as the view model.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Looking to display or conceal several divs according to the option selected from a dropdown menu?

I've been searching for a solution to my simple dropdown issue, but haven't had any luck in the forums. This is the code for the dropdown: <select id ="category_faq"> <option value="1">item1</option> <option value= ...

The HTML content retrieved through an ajax service call may appear distorted momentarily until the JavaScript and CSS styles are fully applied

When making a service call using ajax and loading an HTML content on my page, the content includes text, external script calls, and CSS. However, upon loading, the HTML appears distorted for a few seconds before the JS and CSS are applied. Is there a sol ...

AngularJS Code is displayed upon page refresh or loading

Just starting out with AngularJS. Currently working on retrieving data from a Rest API and displaying it on the page. Here is the code snippet I am using: $http.get(local_url+'/data'). then(function(response) { $scope.data = respon ...

The functionality of wp_script_is in WordPress seems to be malfunctioning when it comes to

I need to load a certain file only after a specific script has finished loading. Wordpress offers a useful method called 'wp_script_is' for detecting if a script has loaded or not. When I use the jquery handle with "done", it functions as expecte ...

How to extract the date value from an HTML date tag without using a datepicker

Utilizing Intel's app framework means there is no .datepicker method available. The following code snippet generates the date widget within my app: <label for="startdate">Start Date:</label> <input type="date" name="startdate" id="star ...

Here is a guide on how to specify function selection values for a total order. By default, the selection will have predetermined values, and upon clicking the sum button,

<tr id=""> <th> <select id="selection" onchange="myFunction()"> <option id="0" value="0">None</option> <option id="1" value="4.00">Women Suit</option> <option id="2" value="10.00">Dres ...

AngularJS - Use promise instead of returning a data object

I am currently working on a project using AngularJS. Within my service.js file, I am attempting to retrieve some values. However, instead of receiving the actual data, I am getting back a promise object with some $$variables along with the desired data. ...

Why are my basic style properties not appearing correctly when I use json_encode on an array?

My calendar is written in Javascript within a table with the ID "calendario," allowing me to manipulate it using calendario.rows[i].cells[i]. This calendar lets users make reservations and provides an option to close a day if there are too many reservatio ...

Retrieve the initial value from the TextField

My website features multiple filters, including by date and duration, allowing users to easily find the information they need from a large dataset. There is also a "reset all filters" button that clears all filters and displays the full list of products. ...

Choosing the relevant kendo row on two different pages

I am facing a situation where I have a Kendo grid displayed on a dashboard page. Whenever a user selects a row in this grid, I need to load the same row in another Kendo grid on a separate ticket page to showcase the details of that particular ticket. The ...

Determine the present height of the current class and substitute it with another class that has the same

My wordpress blog theme has an ajax pagination feature that works well, except for the fact that when a user clicks on the next page link, the entire posts area disappears while the new content is loading. I would like to maintain the same container dimens ...

React's createPortal function does not place the child element inside a container

My new to-do list app in React has a simple functionality where clicking a button should add a new item to the list. However, I am facing an issue with the createPortal method not triggering the UI to render the new item. Here's the code snippet causi ...

The inclusion of <script> tag within the response received from an Ajax

Is there a way to update an element's innerHTML using Ajax.request that includes <script> tags? <div><script type="text/javascript">javascript</script></div> I want to update the content of a div with code from above wi ...

The variable that was posted is not being successfully transferred within the query

There seems to be an issue with retrieving data from the ajax call in ajaxcall.php and assigning it to $place = $_POST['place']; in listplace.php. Despite this problem, the code runs smoothly otherwise. I've tried numerous times to get the ...

Preventing a user from accessing the login page if they are already logged in using Reactjs

I need assistance with implementing a "Login Logout" module in Reactjs using the nextjs framework. My goal is to redirect users to the "dashboard" page if they are logged in (email set in cookie). However, I am encountering an error with the following co ...

Tips for moving a texture horizontally across a sphere using threejs

I have a 360 degree viewer tool. Is there a way to load a texture in a specific position that will rotate the original image by a certain number of degrees or units around the Y-axis without altering anything else about how the picture is displayed? If s ...

In ReactJS, one can create a variable and include a map function by first declaring the variable and then using the map function within the component. If you

Having trouble integrating the accordian.js page into the app.js main page for compilation. Need help defining and writing out the function correctly, any suggestions? Below is my code: App.js: How do I incorporate the components from the accordian.js pa ...

Tabulating with jQuery Arrays

I am facing an issue when trying to perform calculations with certain numbers. Here is the specific code snippet causing trouble: for (var key in week_total) { $("." + key).html(week_total[key]); } After running this code, I keep getting a value of N ...

Troubles with the compatibility of javascript and jquery's multiselect plugin

I've been utilizing the multiselect API to create a dropdown with multiple select options. This is my HTML code: <select id="options" multiple="multiple"></select> And this is my JS code: render:function(){ // $('#viewTemp& ...

Retrieve the element that triggered the event listener within Nuxt

I am currently developing a Nuxt project where I have set up my component using the code below. The select-parent-container has a click event attached to it. Whenever this event is triggered, I need to identify and return the specific parent container that ...