@input 与@output 的初步理解

@input是用来定义输入的,是接收其他组件传过来的数据的。相当于指令的值绑定,无论是单向的(@)还是双向的(=)。都是将父作用域的值“输入”到子作用域中,然后子作用域进行相关处理。我这个是在写一个地址选择框时的一个公用的组建。

export class InputData {
        province:number;
        city:number;
        area:number;
	town:number;
	address:string;
}

@output相当于指令的方法绑定,子作用域触发事件执行响应函数,而响应函数方法体则位于父作用域中,相当于将事件“输出到”父作用域中,在父作用域中处理。

 @Input() data:InputData;
  @Output() areaemit = new EventEmitter<any>();

 这是我的输入输出的格式然后areaemit是我的处理输出的名字

export class AddressInfo {
	provinceId:number;
	//provinceName:string;
	cityId:number;
	//cityName:string;
	areaId:number;
	//areaName:string;
	townId:number;
	//townName:string;
}

  这是定义的输出类里面的四个参数

/**
	*  获取地址Id
    *  @param level 1是省,2是市,3是区,4是镇
	* 
	*/
	public getArea(level:number){
		let areaId:number = 0;
		switch(level){
			case 2: 
				areaId =this.data.province;
				this.selectAddressInfo.provinceId = areaId;
				
				break;
			case 3:
			    areaId  = this.data.city;
				this.selectAddressInfo.cityId = areaId;
				break;
			case 4:
				areaId =this.data.area;
				this.selectAddressInfo.areaId = areaId;
			break;
			case 5:
				areaId =this.data.town;
				this.selectAddressInfo.townId = areaId;
				  
			break;
		}

		this.checkAllSelect();		//如果全部选择了,通知父组件

		this.manageService.getArea(areaId).subscribe(
			res =>{
				if(res['errorCode'] == "0"){
					switch(level){
                        case 1:		//省
							this.provinceList = res['data']['areaInfo'];
						
							if(this.data.province > 0){
								
								this.getArea(2);
							}
							break;
						case 2:		//市
							this.cityList = res['data']['areaInfo'];
							
							
								this.getArea(3);
							
							break;
						case 3:   //区
							this.areaList = res['data']['areaInfo'];
						
							if(this.data.area > 0){
								this.getArea(4);
							}
							break;
						case 4:	 //镇
							this.townList = res['data']['areaInfo'];
				
							if( this.data.province >4 && this.data.province<33){
								this.getArea(5);
							}
							break;

					}
				}else if(res['errorCode'] == "-10000005"){
				}else if(res['errorCode'] == "-20000001"){
					//goToLogin(this.router);
				}else{
					this.toptipsService.warn(res['msg'] + ":" + res['errorCode'])
				}
			},
			error => {
				this.toptipsService.warn("服务器出现错误,请稍后重试!")
			},
			() => {
				
			}
		);
	}

	public checkAllSelect(){
		if(this.selectAddressInfo.provinceId > 0 && this.selectAddressInfo.cityId > 0&&this.selectAddressInfo.townId>0){
			this.areaemit.emit(this.selectAddressInfo);
		}
	}

  在一个检查选择的方法里面把他弹出了。还有前面选择的时候的赋值。这是子组件里面的一部分内容。

然后就是在父组件里面传入.父组件模版的内容

  <app-area [data] = "data.userInfo" (areaemit) = "updateArea($event)"></app-area>

  然后这里做了传出数据赋值

	public updateArea(selectAddress:AddressInfo){
		 selectAddress.provinceId = this.data.userInfo.province;
		 selectAddress.areaId = this.data.userInfo.area;
		 selectAddress.cityId = this.data.userInfo.city;
		 selectAddress.townId = this.data.userInfo.town;
	}

  不过要引入子组件的类

import { AddressInfo } from '../../../../utils/area/area.component'

声明: 这是我刚学Angularjs 然后有很多不足希望大神来指点。

1.我也有很多疑问为什么我不用output输出也能接收到数据啊。(我认为是数据的双向绑定吗)

2.其实我感觉里面代码中也有很多不用的。

 

转载于:https://www.cnblogs.com/zglll/p/7810099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Angular中的@Input@Output是用来实现组件之间通信的注解。 @Input用来接收父组件传递过来的数据,而@Output则用来向父组件传递数据。 下面是它们的使用方法: 1. @Input 在子组件中使用@Input注解来接收父组件传递的数据。例如: ``` import { Component, Input } from '@angular/core'; @Component({ selector: 'child-component', template: ` <div>{{data}}</div> ` }) export class ChildComponent { @Input() data: string; } ``` 在父组件中使用属性绑定的方式将数据传递给子组件。例如: ``` import { Component } from '@angular/core'; @Component({ selector: 'parent-component', template: ` <child-component [data]="myData"></child-component> ` }) export class ParentComponent { myData = 'Hello world'; } ``` 2. @Output 在子组件中使用@Output注解来定义一个事件。例如: ``` import { Component, Output, EventEmitter } from '@angular/core'; @Component({ selector: 'child-component', template: ` <button (click)="onClick()">Click me</button> ` }) export class ChildComponent { @Output() clicked = new EventEmitter<void>(); onClick() { this.clicked.emit(); } } ``` 在父组件中使用事件绑定的方式监听子组件的事件。例如: ``` import { Component } from '@angular/core'; @Component({ selector: 'parent-component', template: ` <child-component (clicked)="onChildClicked()"></child-component> ` }) export class ParentComponent { onChildClicked() { console.log('Child component clicked'); } } ``` 这样就实现了子组件向父组件传递数据的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值