Changing the parent scope from the child component does not automatically reflect changes in an ng-repeat

I am facing an issue with the communication between a child controller and a parent controller. I have a function that adds data to a parent array which is used in an ng-repeat directive.

Although the parent array gets updated correctly and its length is displayed accurately in the parent controller, the ng-repeat directive fails to refresh.

<div ng-controller="parentCtrl">
This shows {{shared.arr.length}} <br/>
This also shows {{shared.arr|json}} <br/>

<div ng-repeat="a in shared.arr">
{{a}} This does not update, it continues to display old data.
</div>

<section ng-contoller="childCtrl">
<button ng-click="test()">Test</button>
</section>
</div>
angular.module('testApp')
  .controller('parentCtrl', function ($scope) {
$scope.shared = {arr:[1,2]};
});
  .controller('childCtrl', function ($scope) {
$scope.test = function(){$scope.shared.arr.push(4);}
});

Answer №1

angular.module('testApp', [])
  .controller('parentCtrl', ['$scope', parentCtrl])
  .controller('childCtrl', ['$scope', childCtrl]);

function parentCtrl ($scope) {
  $scope.shared = {
    arr: [1, 2]
  };
}
function childCtrl ($scope) {
  $scope.test = function (arr) {
    arr.push(4);
  }
}

<div ng-controller="childCtrl">
    <button ng-click="test(shared.arr)">Test</button>
</div>

http://jsfiddle.net/kikill/zhzb3pxh/

Give this code a try.

There were 2 errors in the code: 1) It should be ng-controller="childCtrl", not ng-contoller="childCtrl". 2) You passed the parent's variable into the 'test' function, which can lead to unexpected errors. Consider using the 'controller as' syntax for better organization. Learn more about it here.

Answer №2

After debugging the issue, it seems that the problem in your code lies in the misspelling of "controller" as "contoller" in the line <ng-contoller="childCtrl">. It's a common mistake but an easy fix once identified :)

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

Utilizing Header and Footer Components in React JS

I'm new to Reactjs(Nextjs) and I am looking to create a "header" and "footer" file that can be used on all pages. I would like to know the best approach to achieve this. Should I create a "Layout.js" file and then call the Header within it? Or should ...

What methods can I use to modify strings within JSX?

Within a JSX file, I am faced with the task of manipulating a particular string in a specific manner: Imagine that I have the following string assigned to medical_specialty = "Plastic Surgery" The objective is as follows: medical_specialty.replace(&apos ...

Adjust the jQuery and PHP script to retrieve both the ID and text content to populate an OPTION element

I have a basic DROPDOWN list: <select class='form-control' id='builders' name='builder_id'> <option value="1">Java</option> <option value="2">Python</option> </select> I am looking ...

Ways to manually update a counter while using ng-repeat and ng-if in AngularJS

Presently, I am using the following typical loop structure <div ng-repeat= <li ng-if= --> {{$index}} </br> However, using $index does not quite meet my requirements as it outputs --> 1 --> 2 --> 5 --> 9 ... What I ...

What is the process for redirecting to a different URL when the button is clicked?"

I am trying to display Home.js at localhost/posts, but when the button is clicked, it displays information at the auth.js URL. How can I link the button so that if the login information is correct, it redirects to "localhost/posts" instead of rendering its ...

Trouble with mouseout listener in Wordpress causing Google Map to malfunction

Having trouble integrating a map into my WordPress site using the Google Maps API v3. The issue I am facing is that the listener assigned to the mouseout event is not functioning properly. I have copied and pasted the code from another website where it was ...

Tips for managing numerous Axios requests in JavaScript

I have a scenario where I need to send 3 axios calls simultaneously from the frontend to the backend using the axios.all function to modify a MONGO DB database. The challenge I am facing is ensuring that if one of the requests fails, none of the other requ ...

toggle visibility of <li> elements using ng-repeat and conditional rendering

I have an array of color IDs and codes that I'm utilizing with ng-repeat in the <li> tag to showcase all colors. However, I only want to display colors where the color code is less than 10, hiding any colors where the color code exceeds 10. Addi ...

Concealing server controls until Angular successfully loads a customized multi-select dropdown

Currently, I am facing an issue while working with asp.net webform and angular. My page contains a few asp.net server-side controls alongside an angular multi-select dropdown that triggers a server-side function to fetch data. The problem is that the serve ...

Can Express.Multer.File be inserted into a FormData object easily?

Can anyone assist me in figuring out how to add a file of type Express.Multer.File into a FormData object? Situation: I have received a file with the type Express.Multer.File (I am using nestjs and followed this part of the documentation: https://docs.nes ...

Which is the best choice for a large-scale production app: caret, tilde, or a fixed package.json

I am currently managing a sizeable react application in production and I find myself undecided on whether it is more beneficial to use fixed versions for my packages. Some suggest that using the caret (^) is a safer route, but I worry that this could poten ...

An error occurs in the console stating 'Maximum call stack size exceeded' when trying to add data to the database using JavaScript

I've been struggling with a JavaScript error for the past few days and despite scouring through numerous answers on StackOverFlow, none seem to address my specific issue. My goal is to simply submit a record to the database using a combination of Jav ...

determining the quantity within a collection of items

Is there a way to determine the order of an element within a set when clicked? For example, if I click on the third element in a set, can I get it to display as three? Check out this jsfiddle example for reference: http://jsfiddle.net/vtt3d/ ...

Setting the marker position in an Ionic Google Map: A simple guide

I'm experiencing a specific issue with my app that involves Google Maps and markers. Whenever I open the view, my current position is displayed on the screen instead of the marker location. What I actually want is for the map to open directly at the ...

Newbie: Troubleshooting Vue Errors - "Vue is not recognized"

I'm currently at the beginning stages of learning Vue and am practicing implementing it. However, I keep encountering the errors "vue was used before it was defined" and "Vue is not defined". Below are excerpts from my HTML and JS files for reference. ...

Utilizing the power of Material-UI with React in conjunction with Python-Django: A comprehensive

I am seeking guidance on implementing React with Material UI components in my web application. The technologies I have utilized in multiple projects include: Materialize CSS, Javascript, Jquery. The technologies I wish to explore for future projects are ...

Increase the worth of current value

Whenever a user enters their name into an input box, I want it to be shown after the word 'hello'. However, currently, the word 'hello' gets replaced by the user's name instead of being displayed after it. var name = document.ge ...

Explore the hidden route of the input components

HTML: <div id="quiz"> <div id="question"> <p id="quiz-txt">What is your favorite color?</p> <ul id="quiz-opt"> <div id="ans"> <input type="checkbox" id="Red" value="Red" class="options"> ...

What is the best way to incorporate an AppBar featuring a Back to Top button from Material UI for React into my application?

While exploring the Material UI documentation, I came across this interesting code snippet: import React from 'react'; import PropTypes from 'prop-types'; import AppBar from '@material-ui/core/AppBar'; import Toolbar from &ap ...

What steps can I take to optimize my code and eliminate any redundancies?

In a current project, I need to make calls to three different endpoints. The functions handling these requests are identical except for the URLs being called. Below is an example of the code: const handleSubmitRequest = (e, next, endpoint, requestData) =&g ...