【Angular】Angular子组件接受参数时使用set、get

1. @Input来接受父组件传值—及获及用

angular子组件中使用@Input来接受父组件传值,最常见的是“及获及用”,代码如下:
父组件parent

nameCon: 'yyy' //ts中
<app-child [name]="nameCon"></app-child> //html中

子组件child

import { Input } from '@angular/core';
@Input() name: string;  //子组件接受name

子组件或指令中的 @Input()装饰器表示该属性可以从其父组件中获取值。
要想监视@Input()属性的变化,你可以使用 Angular 的生命周期钩子OnChanges
但上述方法,对于接收到的值都是,收到就使用。如果想对接收到参数进行一些处理 ,或者是根据参数执行不同操作。这个时候可以使用set和get方法。

2. 对接受的值进行再次处理

父组件parent

nameCon: 'yyy' //ts中
<app-child [name]="nameCon"></app-child> //html中

子组件child

//ts文件中
import { Input } from '@angular/core';
private nameStr: string;  // 从父组件传过来的name值,进行大写转化,被重新赋值的参数
@Input()
  set name(value: string) {
    this.nameStr = value.toUpperCase();
  }
  get name(): string {
    return this.nameStr;
  }


{{ name }}  //html中YYY

要重命名一个参数用来承接被处理后的值。因为同名会报错error:超过了最大堆栈大小.
在这里插入图片描述

3.根据不同的参数结果执行不同的方法

我将在父组件中创建一个Select,从而达到传给子组件不同值的效果

父组件parent

<form [formGroup]="validateForm">
   <nz-form-item>
        <nz-form-label>城市</nz-form-label>
        <nz-form-control>
          <nz-select
            formControlName="optionCity"
            nzAllowClear
            nzPlaceHolder="请任意选择一个城市"
            (ngModelChange)="changed($event)"
          >
            <nz-option nzValue="1" nzLabel="北京"></nz-option>
            <nz-option nzValue="2" nzLabel="上海"></nz-option>
            <nz-option nzValue="3" nzLabel="广州"></nz-option>
          </nz-select>
        </nz-form-control>
    </nz-form-item>
</form>

<app-child [selectedValue]="optionCity"></app-child>

import { FormBuilder, FormGroup } from '@angular/forms';
optionCity = '1'; // 默认北京
constructor(private fb: FormBuilder) { }

ngOnInit() {
  this.validateForm = this.fb.group({
    optionCity: [this.optionCity, []]
  });
}

changed(value: string): void {
    this.optionCity = value;
}

子组件child

import { Input } from '@angular/core';

private _selectedValue: string;

@Input()
  set selectedValue(value: string) {
    this._selectedValue = value;
    if (this._selectedValue === '1') {
      this.aaa();
    } else if (this._selectedValue === '2') {
      this.bbb();
    } else {
      this.ccc();
    }
  }
  get selectedValue(): string {
    return this._selectedValue;
  }
aaa() {
   console.log('aaaaaaaaaaaaaaaaaaa');
}
bbb() {
   console.log('bbbbbbbbbbbbbbbbbbb');
}
ccc() {
   console.log('ccccccccccccccccccc');
}

如果你在父组件中的select下拉框改变了选项值,那么子组件这里就会相对应的执行相对应的操作

https://blog.csdn.net/qq_39961695/article/details/108196614

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Expert Angular by Mathieu Nayrolles English | 31 July 2017 | ISBN: 1785880233 | ASIN: B01M8JSW2M | 454 Pages | AZW3 | 7.75 MB Learn everything you need to build highly scalable, robust web applications using Angular release 4 About This Book Apply best practices and design patterns to achieve higher scalability in your Angular applications Understand the latest features of Angular and create your own components Get acquainted with powerful, advanced techniques in Angular to build professional web applications Who This Book Is For This book is for JavaScript developers with some prior exposure to Angular, at least through basic examples. We assume that you've got working knowledge of HTML, CSS, and JavaScript. What You Will Learn Implement asynchronous programming using Angular Beautify your application with the UI components built to the material design specification Secure your web application from unauthorized users Create complex forms, taking full advantage of 2-way data binding Test your Angular applications using the Jasmine and Protractor frameworks for better efficiency Learn how to integrate Angular with Bootstrap to create compelling web applications Use Angular built-in classes to apply animation in your app In Detail Got some experience of Angular under your belt? Want to learn everything about using advanced features for developing websites? This book is everything you need for the deep understanding of Angular that will set you apart from the developer crowd. Angular has introduced a new way to build applications. Creating complex and rich web applications, with a lighter resource footprint, has never been easier or faster. Angular is now at release 4, with significant changes through previous versions. This book has been written and tested for Angular release 4. Angular is a mature technology, and you'll likely have applications built with earlier versions. This book starts by showing you best practices and approaches to migrating your existing Angular applications so that you can be immediately up-to-date. You will take an in-depth look at components and see how to control the user journey in your applications by implementing routing and navigation. You will learn how to work with asynchronous programming by using Observables. To easily build applications that look great, you will learn all about template syntax and how to beautify applications with Material Design. Mastering forms and data binding will further speed up your application development time. Learning about managing services and animations will help you to progressively enhance your applications. Next you'll use native directives to integrate Bootstrap with Angular. You will see the best ways to test your application with the leading options such as Jasmine and Protractor. At the end of the book, you'll learn how to apply design patterns in Angular, and see the benefits they will bring to your development. Style and approach This book provides comprehensive coverage of all aspects of development with Angular. You will learn about all the most powerful Angular concepts, with examples and best practices. This book is everything you need for the deep understanding of Angular that will set you apart from the developer crowd.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卸载引擎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值