Is there a specific reason why Angular2 always allows for the creation of a unidirectional data flow?

Here is a great article that I came across.

The article explains the speed difference between angular1 and angular2, stating that one reason for the faster performance of angular2 is its ability to create a unidirectional change detection tree.

However, my confusion arises when considering components with @output and eventEmitter, as the data flow in such cases is inherently non-unidirectional due to the possibility of data flowing back to the parent component.

For instance,

Child Component

@Component({
  selector: 'Child',
   inputs: ['myfullname'],
   outputs: ['changefirstname']
})
@View({
 template: `
   <div (click)="changefirstname()">
     {{myfullname}}
   </div>
 `
 })

 export class Child {
     public myfullname: String; 
     public myevent: EventEmitter = new EventEmitter();

     changefirstname(evt) {
        this.myevent.next('newfirstname');
     }
}

Parent Component

@Component({
  selector: 'Parent',
  directives: [Child]
})
@View({
 template: `
   <Child [myfullname]={{myfullname}} (myevent)="handleMyEvent($event)"></Child>
 `
 })

 export class Parent {

   this.myfullname = 'default';

   handleMyEvent(arg) {
     this.myfullname = arg;
   }
 }

In this simple setup, clicking on the child component updates the name in the parent component. However, since the child receives the name from the parent, it also gets updated. This demonstrates a non-unidirectional data flow, as it is not strictly top-down.

We can even consider a more complex scenario like:

    A
  /   \
 B     C

Where C emits an event to

A</code, resulting in a model change that affects both <code>C
and B. Focusing solely on the subtree of C during change detection would overlook the model changes in A and B.

So, is the article incorrect? Or am I overlooking something?

Answer №1

data has the ability to return to its source

That's correct. However, information is transmitted as an event back to the original source, while data flows downwards from the source to its offspring.

In essence, the child does not alter the original data. It is only the parent (or the entity possessing the data) that can do so.

Answer №2

When a value undergoes a change, the subsequent change detection cycle identifies it and updates bindings from parent to child inputs.

If an output triggers an event, the parent is then refreshed

resulting in a new change detection cycle being initiated.

This indicates that change detection itself only forwards changes in a single direction (unidirectional) from parent to child.
Changes in the opposite direction are transmitted through other means (events), disrupting any cycles that would arise if change detection also handled bidirectional changes.

For further information, refer to

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

selenium encounters an "option element does not belong to a select" exception

Currently, I am working on creating automation using Selenium for an Angular 2 application. One of the functionalities in this application involves a dropdown that filters options as the user starts typing in a designated field. The HTML structure for this ...

Creating a variety of active button colors using Bootstrap and Angular's ngFor

Is there a way to assign individual colors to each button in a menu with four buttons? I want different colors for the active state and rollover effect of each button. https://i.sstatic.net/3rW0L.png .html <div class="d-flex justify-content-center"&g ...

Issue with displaying entire object using Jest and console.dir

I'm having trouble displaying an error in my Jest test because it's not showing all the levels as expected. import util from 'util' describe('Module', () => { it('should display all levels WITHOUT util', () =& ...

The necessary directive controller is missing from the element in the current DOM structure

Can anyone explain the meaning of "required directive controller is not present on the current DOM element"? I encountered this error and would like some clarity. For reference, here is the link to the error: https://docs.angularjs.org/error/$compile/ctr ...

Is it possible to expand the Angular Material Data Table Header Row to align with the width of the row content?

Issue with Angular Material Data Table Layout Link to relevant feature request on GitHub On this StackBlitz demo, the issue of rows bleeding through the header when scrolling to the right and the row lines not expanding past viewport width is evident. Ho ...

Encountered a challenge in Angular 2.4 when attempting to import a feature module from another project into the main project

I currently have two distinct projects, one serving as the main project and the other as a shared project. Within the shared project, I have developed a feature module known as NavBar. The intention behind separating these projects is to facilitate the re ...

What is preventing me from downgrading Rxjs?

Looking for a solution to downgrade my rxjs package from version 6.1.0 to 5.5.4. Here's what I've tried so far: npm -v rxjs 6.1.0 npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="73010b190033465d465d47"> ...

Do I need to convert AngularJS to .ts for an Angular/AngularJS hybrid application?

Currently in the process of upgrading from AngularJS v1.25 to Angular 14 using the ng-Upgrade approach outlined on angular.io/guide/upgrade. Adding an extra challenge, our main page is built with ASP.NET MVC 5, and I am aiming to incorporate the Angular CL ...

Transferring data between components in React by passing parameters through Links

When calling another component like <A x={y}/>, we can then access props.x inside component A. However, in the case of calling EditCertificate, the id needs to be passed to the component. I am using a Link here and struggling to pass the id successf ...

Combining Typescript interfaces to enhance the specificity of a property within an external library's interface

I have encountered a scenario where I am utilizing a function from an external library. This function returns an object with a property typed as number. However, based on my data analysis, I know that this property actually represents an union of 1 | 2. Ho ...

Exploring the use of global variables in React

Welcome to my React learning journey! I've encountered an issue while trying to access a global variable exposed by a browser extension that I'm using. Initially, I attempted to check for the availability of the variable in the componentDidMount ...

Enhance the functionality of 'takeWhile' by incorporating a limit parameter, similar to how 'take' operates

I am attempting to retrieve all pages until either there are no more pages or a certain limit (let's say 10 pages) is reached. If I follow this approach: obs.pipe(expand((page) => { return service.call(page).nextPage; }), take(10), takeWhil ...

How can we utilize Typescript to check if the intern 4 page has finished loading?

I've managed to set up a function in intern 4 using TypeScript that waits for the page to load. However, there are instances where it doesn't work and throws a TimeOutError even when I catch the error within the function. Can someone please take ...

TypeScript overloading error: Anticipated 0 parameters, received 2 instead

I am facing an issue with a class containing an overloaded method that has two versions. One version does not take any arguments, while the second one can take two arguments. class DFD { ... getEndDatetime(): string; getEndDatetime(startTime?: ...

What is the process of implementing session storage in an AngularJS directive?

I am trying to save values in Angular session storage within an Angular directive, but I am facing an issue where I only receive NULL. Can anyone provide assistance? app.directive('myDirective', function (httpPostFactory) { return { restrict ...

Issue with form.io: Custom component not displaying on the form

I'm having trouble implementing form.io with a custom component named MeComponent. Unfortunately, it doesn't seem to be rendering properly and I can't pinpoint what's missing. Here are the steps I've taken. app.module.ts import { ...

Retrieve the mfData value from the TypeScript file in order to perform operations on it within the Angular 2 framework

I have a snippet of code that iterates through data from stacklist_table, which is a JSON array, and displays it in a table format. The stacklist_table contains a full list of objects, but I only need a subset of these objects so I have applied some filter ...

Angular: When making an HTTP GET request, receiving an OPTIONS 405 error message indicating that the method is

I'm facing an issue with my API when making an HTTP GET request - it returns OPTIONS 405 (Method Not Allowed). Access to XMLHttpRequest at 'apiurl' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to ...

After importing this variable into index.ts, how is it possible for it to possess a function named `listen`?

Running a Github repository that I stumbled upon. Regarding the line import server from './server' - how does this API recognize that the server object has a method called listen? When examining the server.ts file in the same directory, there is ...

"Encountering a 500 error on Chrome and Internet Explorer while trying to sign

I am currently working on an ASP.NET Core application that handles identity management through Azure AD B2C using the ASP.Net Core OpenID Connect. The front end is developed using AngularJS 2 with TypeScript. In my Logout function, the user is redirected t ...