Home Web Front-end JS Tutorial Detailed explanation of the input properties and output properties of Angular4

Detailed explanation of the input properties and output properties of Angular4

Dec 12, 2017 am 10:51 AM
Attributes output

This article mainly introduces the input properties and output properties of Angular4. It analyzes the concepts, functions and related usage skills of Angular4 input properties and output properties in detail in the form of examples. Friends in need can refer to it. I hope it can help everyone.

Angular4 input attribute

The input attribute is usually used to transfer information from the parent component to the child component

For example: we pass the stock code from the parent component to the child component, here We call the child component app-order

First declare the value that needs to be passed in from the parent component in app.order.component.ts

order.component.ts


...
@Input()
stockCode: string
@Input()
amount: string
...
Copy after login


order.component.html


<p>这里是子组件</p>
<p>股票代码为{{stockCode}}</p>
<p>股票总数为{{amount}}</p>
Copy after login


Then We need to pass the value to the child component in the parent component (app.component)

app.component.ts


...
stock: string
...
Copy after login


app.component.html


<input type="text" placeholder="请输入股票代码" [(ngModel)]="stock">
<app-order [stockCode]="stock" [amount]="100"></app-order>
Copy after login


Here we use Angular’s ​​two-way data binding to combine the user input value and control Bind the stock in the browser. Then it is passed to the subcomponent, and the subcomponent displays it on the page after receiving it.

Angular4 Output Properties

Output properties are needed when a child component needs to pass information to the parent component.

For example: when we obtain the real-time price of a stock from the stock exchange, we hope that this information can also be obtained externally. For convenience, the real-time stock price here is simulated by a random number. We call the subcomponent here app.price.quote

Use EventEmitter to emit events from the subcomponent

price.quote.ts


export class PriceQuoteComponent implements OnInit{
 stockCode: string = 'IBM';
 price: number;
 //使用EventEmitter发射事件
 //泛型是指往外发射的事件是什么类型
 //priceChange为事件名称
 @Output()
 priceChange:EventEmitter<PriceQuote> = new EventEmitter();
 constructor(){
  setInterval(() => {
   let priceQuote = new PriceQuote(this.stockCode, 100*Math.random());
   this.price = priceQuote.lastPrice;
   //发射事件
   this.priceChange.emit(priceQuote);
  })
 }
 ngInit(){
 }
}
//股票信息类
//stockCode为股票代码,lastPrice为股票价格
export class PriceQuote{
 constructor(public stockCode:string,
    public lastPrice:number
 )
}
Copy after login


price.quote.html


<p>
 这里是报价组件
</p>
<p>
 股票代码是{{stockCode}}
</p>
<p>
 股票价格是{{price | number:'2.2-2'}}
</p>
Copy after login


Then we receive it in the parent component Event

app.component.html


<app-price-quote (priceChange)="priceQuoteHandler($event)"></app-price-quote>
<p>
 这是在报价组件外, 股票代码是{{priceQuote.stokcCode}},
 股票价格是{{priceQuote.lastPrice | number:'2.2-2'}}
</p>
Copy after login


Event binding is the same as native event binding. All put the event name in ().

app.component.ts


export class AppComponent{
 priceQuote:PriceQuote = new PriceQuote('', 0);
 priceQuoteHandler(event:PriceQuote){
  this.priceQuote = event;
 }
}
Copy after login


The event type here is the type of event passed by the subcomponent.

To put it simply, the child component emits the priceChange event through emit and passes the value out. When the parent component uses the child component, it will trigger the priceChange event and receive the value.

Related recommendations:

Project preparation and environment building operations in Angular4

CSS style example code for how to display content in Angular4

Detailed explanation of examples of routing Router class in Angular4

The above is the detailed content of Detailed explanation of the input properties and output properties of Angular4. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get integer literal properties in Python without SyntaxError? How to get integer literal properties in Python without SyntaxError? Aug 20, 2023 pm 07:13 PM

TogetintliteralattributeinsteadofSyntaxError,useaspaceorparenthesis.TheintliteralisapartifNumericLiteralsinPython.NumericLiteralsalsoincludesthefollowingfourdifferentnumericaltypes−int(signedintegers)−Theyareoftencalledjustintegersorints,arepositiveo

Python's dir() function: View the properties and methods of an object Python's dir() function: View the properties and methods of an object Nov 18, 2023 pm 01:45 PM

Python's dir() function: View an object's properties and methods, specific code example required Summary: Python is a powerful and flexible programming language, and its built-in functions and tools provide developers with many convenient features. One of the very useful functions is the dir() function, which allows us to view the properties and methods of an object. This article will introduce the usage of the dir() function and demonstrate its functions and uses through specific code examples. Text: Python’s dir() function is a built-in function.

How to rename properties of JSON using Gson in Java? How to rename properties of JSON using Gson in Java? Aug 27, 2023 pm 02:01 PM

The Gson@SerializedName annotation can be serialized to JSON and have the provided name value as its field name. This annotation can override any FieldNamingPolicy, including the default field naming policy that may have been set on the Gson instance. Different naming strategies can be set using the GsonBuilder class. Syntax@Retention(value=RUNTIME)@Target(value={FIELD,METHOD})public@interfaceSerializedNameExample importcom.google.gson.annotations.*;

What to do if Win11 disk properties are unknown What to do if Win11 disk properties are unknown Jul 03, 2023 pm 04:17 PM

What should I do if the disk properties of Win11 are unknown? Recently, Win11 users found that the system prompted a disk error when using their computers. What is going on? And how to solve it? Many friends don’t know how to operate in detail. The editor has compiled the steps to solve the Win11 disk error below. If you are interested, follow the editor to read below! Steps to solve Win11 disk error 1. First, press the Win+E key combination on the keyboard, or click the File Explorer on the taskbar; 2. In the right sidebar of the File Explorer, find the side and right-click the local disk (C :), in the menu item that opens, select Properties; 3. Local disk (C:) Properties window, switch to Tools

bottom attribute syntax in CSS bottom attribute syntax in CSS Feb 21, 2024 pm 03:30 PM

Bottom attribute syntax and code examples in CSS In CSS, the bottom attribute is used to specify the distance between an element and the bottom of the container. It controls the position of an element relative to the bottom of its parent element. The syntax of the bottom attribute is as follows: element{bottom:value;} where element represents the element to which the style is to be applied, and value represents the bottom value to be set. value can be a specific length value, such as pixels

Methods and examples of dynamically adding attributes using the Vue.set function Methods and examples of dynamically adding attributes using the Vue.set function Jul 24, 2023 pm 07:22 PM

Methods and examples of using the Vue.set function to dynamically add properties. In Vue, if we want to dynamically add a property to an existing object, we usually use the Vue.set function. The Vue.set function is a global method provided by Vue.js, which can ensure responsive updates when adding properties. This article will introduce the use of Vue.set and provide a specific example. First of all, in Vue, we usually use the data option to declare responsive data.

Introduction to the attributes of Hearthstone's Despair Thread Introduction to the attributes of Hearthstone's Despair Thread Mar 20, 2024 pm 10:36 PM

Thread of Despair is a rare card in Blizzard Entertainment's masterpiece &quot;Hearthstone&quot; and has a chance to be obtained in the &quot;Wizbane's Workshop&quot; card pack. Can consume 100/400 arcane dust points to synthesize the normal/gold version. Introduction to the attributes of Hearthstone's Thread of Despair: It can be obtained in Wizbane's workshop card pack with a chance, or it can also be synthesized through arcane dust. Rarity: Rare Type: Spell Class: Death Knight Mana: 1 Effect: Gives all minions a Deathrattle: Deals 1 damage to all minions

How to output text with line breaks in Go language How to output text with line breaks in Go language Mar 15, 2024 pm 04:15 PM

Go language is a modern, efficient and concise programming language that is widely used in software development in various fields. In the Go language, outputting text with newlines is very simple and can be achieved by using the Println function provided by the fmt package. Below we will introduce in detail how to output text with line breaks in Go language, as well as related code examples. In the Go language, if you want to output text with newlines, you can use the Println function provided by the fmt package. The Println function will output text in

See all articles