How to implement two-way data binding in Angular

亚连
Release: 2018-06-21 16:58:11
Original
1347 people have browsed it

The editor below will share with you an example of two-way data binding using Angular custom components. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor and take a look.

Students who have studied Angular all know that the input box implements two-way data binding through[(ngModel)], so can custom components implement two-way data binding? The answer is yes.

In Angular, we often need to pass square brackets[]and round brackets()Realize interaction between components:

Then in[]## Based on # and(), how to implement two-way data binding of components?

Examples are as follows.

Subcomponent:

 

childStatus: {{childStatus}}

Copy after login
//testDataBinding.component.ts export class TestDataBindingComponent implements OnInit{ @Input() childStatus; @Output() childStatusChange = new EventEmitter(); ngOnInit(){ setTimeout(()=>{ this.childStatus = false; this.childStatusChange.emit(this.childStatus); },5000); } }
Copy after login

Pay attention to the writing here, this is the key, the first half of the output attribute must be the same as the input attribute, the input attribute You can name it arbitrarily. The output attribute needs to add Change to the input attribute. For example, if your input attribute is myData, then the output attribute must be myDataChange.

Parent component:

  

parentStatus: {{parentStatus}}

Copy after login
//app.component.ts import { Component,OnInit } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements OnInit{ parentStatus: boolean = true; ngOnInit(){ setTimeout(()=>{ this.parentStatus = true; },10000); } }
Copy after login

In the parent component we initialize

parentStatus totrue and pass it to the child componentTestDataBindingComponent.

In the child component, after 5 seconds we setchildStatus tofalse , see if it can be passed to the parent component. After another 5 seconds, we setparentStatus totrue in the parent component to see if it can be passed to the child component.

It turns out that after the value of the child component changes, the value of the parent component also changes; after the value of the parent component changes, the value of the child component also changes!

We have implemented two-way binding!

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to implement dynamic loading of histograms in angularjs

How to use scope in Angular scope

How to implement menu permission control using react

Detailed explanation of how props pass parameters in vue.js

The above is the detailed content of How to implement two-way data binding in Angular. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!