Differentiating the angular distinction between setting a variable with ng-click and invoking a function

I've encountered a situation like this before.

Let's assume the controller has these variables:

$scope.valueArr = ['Hello', 'No', 'Yes'];
$scope.myValue = 'Hello';

And there is an ng-repeat as follows:

<li ng-repeat="value in valueArr" ng-class="{'active' :  myValue == value}"><a>{{value}}</a> </li>

If I do this:

<a ng-click="myValue = value"> </a>

The new element gets the class, but the previous one loses it.

However, if I try this instead:

<a ng-click="setMyValue(value);"> </a>

and define the function in the controller:

$scope.setMyValue = function(value){$scope.myValue = value}

Only the last clicked element stays active, showing that a function is necessary for re-rendering. Can someone explain why this happens and when to use each method?

This case is just one example where re-rendering issues come up.

Answer №1

One reason for this behavior is that when using ng-repeat, a new child scope is created for each repetition, as explained in the documentation.

This means that each repeated item has its own separate scope.

There is a distinct child scope for the first repetition.

There is a distinctive child scope for the second repetition.

There is a unique child scope for the third repetition.

...and so on.

Therefore, if you try to access a variable like (myValue) using

ng-click="myValue = value"
, Angular will search within the current repetition's child scope. If it doesn't find any existing reference to myValue, it will create one within that specific child scope. However, you can still access variables from the parent scope by using syntax like $parent.myValue.

You can test this behavior with the following demo.


Another approach to handle this situation is to use an object, for example:

$scope.tempObj = {myValue: 'Hello'};

Then, you can refer to this object property by using:

<a ng-click="tempObj.myValue = value">

Since myValue is now a property of the object tempObj, if Angular cannot find tempObj in the child scope, it will automatically look through the parent scopes.


If you opt to use a function like:

<a ng-click="setMyValue(value);"> </a>

&

$scope.setMyValue = function(value){$scope.myValue = value}

In the setMyValue function, it accesses $scope.myValue from the controller scope directly, rather than relying on the repeat's child scope.

Answer №2

The use of ng-repeat results in the creation of a distinct child scope for each repeated element. For instance, when you input

<a ng-click="myValue = value"> </a>

What occurs is the generation of a new variable 'myValue' within the child scope of the <li> after the user triggers the button. This action conceals the original value of 'myValue' within the parent scope without altering it - effectively causing all other <li> scopes to continue inheriting the original value from the parent scope.

On the contrary, if you were to write

<a ng-click="setMyValue(value);"> </a>

You would be directly invoking the function setMyValue that was established within the parent scope. Consequently, when the button is clicked, the setMyValue function executes and modifies the 'myValue' attribute within the parent scope. The child scope remains unaffected by this adjustment. With the alteration occurring within the parent scope, all instances of <li> elements inherit the updated value accordingly.

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

Is it not possible to utilize async/await with MongoDB Model, despite it yielding a Promise?

Currently, I am facing an issue with a function in my code that is supposed to retrieve a user's inventory and then fetch the price property of each element using data from a Price Collection. Below is a snippet of the function (not the entire thing, ...

Exclude specific fields when updating a document in Firebase using the update()

Currently, I am storing a class in Firebase by using the update() function. Is there a way to stop specific fields (identified by name) of the object from being saved to the Firebase database? It's similar to how we use the transient keyword in Java ...

Guide on parsing a JSON object in JavaScript

One issue I'm facing is that my controller method returns a string representation of a jsonArray using the jsonArray.toString() function. Below is the corresponding ajax method: function loadPropertyFile(url) { $.ajax({ type: "GET", url: url, ...

Having trouble creating a report with an HTML screenshot using Protractor

Need assistance with generating reports using a html screenshot in Protractor. I have followed all the necessary steps but encountered an error. Any help would be appreciated. Here is my conf.js: // Sample configuration file. var HtmlReporter = require(& ...

if considering an integer value of 0 as equivalent to null

I am struggling with using axios to send data to an API in react. Despite the server successfully receiving the values, my code is not entering the if block as expected. Interestingly, when I make the same request from a rest client, it works perfectly. He ...

The process of setting up a carousel for tables using jQuery

I can successfully implement jquery for an image carousel, but now I need to find a way to create a sliding table format. Any assistance on this matter would be greatly appreciated. Here is the code I currently have: <ul> <li><a style= ...

Extract the array of numbers from the JSON file

My task is to extract an array of numbers from JSON files. I need to handle files that contain only arrays of numbers or one level deeper (referred to as direct arrays). These files may also include other data types such as strings or booleans. The challe ...

"Bringing in" ES6 for Node

I am interested in utilizing import from ES6 instead of require from common.js in Node. Surprisingly, I initially assumed that import would function automatically in Node. However, it appears that it does not. Is there a specific npm package that needs t ...

Month and year selection feature in ExtJS 4 catalog

I recently came across an interesting Fiddle that featured a Month and Year picker for apps. After finding this Fiddle here, I noticed that it was built using Extjs 5.0, and it worked perfectly. However, when attempting to switch it to version 4.2.1, the l ...

Which is better for privacy: underscored prototype properties or encapsulated variables?

There's something that's been on my mind lately - it seems like people are aware of something that I'm not. Let's take a look at an example in FOSS (simplified below)... When creating a class in JavaScript, I personally prefer Crockford ...

Press on the button that is currently in your field of view

I have a web page with multiple buttons inside div elements. I am looking to automate the process of clicking the "Buy" button that is currently visible on the screen when the user presses the B key. $(document).keydown(function(e) { if (e.keyCode == ...

Vue.js is experiencing functionality issues on mobile devices and other connected devices, however, it is operating smoothly on desktop devices when localhost is running

I recently ran into an issue while trying to run my Laravel project on localhost and connect it to other devices like mobiles and desktops/laptops using the local address. The function works perfectly on the hosting desktop but fails to work on other devic ...

Pressing the Add button will create a brand new Textarea

Is it possible for the "Add" button to create a new textarea in the form? I've been searching all day but haven't found any logic to make the "Add" function that generates a new textarea. h1,h2,h3,h4,h5,p,table {font-family: Calibri;} .content ...

Utilizing AngularJS to selectively filter objects based on specific fields using the OR operator

My collection includes various items with different attributes. For instance, here is the information for one item: {"id":7,"name":"ItemName","status":"Active","statusFrom":"2016-01-04T00:00:00","development":"Started","devStartedFrom":"2016-01-04T00:00:0 ...

React: Avoid the visible display of conditional rendering component fluctuations

In my current React project, I am developing a page that dynamically displays content based on the user's login status. The state variable "loggedIn" is initially set to null, and within the return statement, there is a ternary operator that determine ...

Setting up a Node.js application with Nginx on DigitalOcean

While running my application on a DigitalOcean droplet using nginx, I encountered a peculiar issue. The app runs perfectly fine with http, but when switching to https, nginx throws a 502 BAD GATEWAY error. Despite trying various DigitalOcean guides and sco ...

My table elements are automatically being populated with <tr> elements thanks to Javascript

Upon viewing the screenshot: it is evident that each element is placed within its own tr. My preference is to have each element contained in a td and then wrap everything within a single tr. Here is the updated HTML structure: <div id='result&a ...

Leveraging the result of one ajax function within a different ajax function

My current project involves the following steps: 1. User creates a template with various elements. 2. When the user clicks a button: *The first ajax function establishes a new entry in the custom template database. *The second ajax function retrieves the ...

Implementing ajax functionality for a form component in vuejs

I am currently working with a Vue.js component that serves as a form with a single field for an email input. The default value of this field, "email", is provided to the component by the parent as a prop. Upon form submission, I need to trigger an event to ...

ng-map displays a portion of a map within the user interface

I'm currently using ng map and getting the UI for half of the screen, but not filling the entire div. Can someone please assist me with this issue? https://i.stack.imgur.com/u2CDn.jpg var app = angular.module('app', ['ngMap']); ...