首页 > 其他分享 >angular开发从入门到入土第二节(组件通信)

angular开发从入门到入土第二节(组件通信)

时间:2023-04-16 23:35:57浏览次数:39  
标签:my component 入土 ChangeDetectionStrategy 组件 第二节 com angular

一.input和output

1.子组件通过@Input装饰器获取到父组件传递的值

//子组件html模板

<p>child</p>
<div>
  {{ showText }}
</div>

子组件类

import { ChangeDetectionStrategy, Component, Input } from '@angular/core';

@Component({
  selector: 'app-my-com',
  templateUrl: './my-com.component.html',
  styleUrls: ['./my-com.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComComponent {
  @Input() showText = ''
}

父类通过输入框来改写传递的值

效果:在父组件的值修改的值传递到子组件

 

 

 2.子组件通过@output装饰器把值传递到父组件

父组件html模板

<p>father</p>
<input nz-input [(ngModel)]="inputText" />
<div> childText {{childText}}</div>
<app-my-com [showText]="inputText" (changeShowText)="childgetFn($event)"></app-my-com>

父组件类

import { ChangeDetectionStrategy, Component } from '@angular/core';
@Component({
  selector: 'app-page1',
  templateUrl: './page1.component.html',
  styleUrls: ['./page1.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class Page1Component {
  inputText = ''
  childText = ''
  childgetFn($event:any){
    this.childText = $event
  }
}

子组件模板

<p>child</p> <div>   chiidshow{{ showText }} </div> childinput<input nz-input [(ngModel)]="showText" (ngModelChange)="childChange()">

子组件类

import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';

@Component({
  selector: 'app-my-com',
  templateUrl: './my-com.component.html',
  styleUrls: ['./my-com.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyComComponent {
  @Input() showText = ''
  @Output() changeShowText = new EventEmitter()
  childChange(){
    this.changeShowText.emit(this.showText)
  }
}

 子组件输入框通过output触发父类方法,父类以此获取到子组件传递的值

3.通过input和output实现父子组件双向绑定

父组件

 子组件

 子组件类

效果

 Angular 中 [] 实现了模型到视图的数据绑定,() 实现了视图到模型的事件绑定。把它们两个结合在一起 [()] 就实现了双向绑定。也被称为 banana in a box 语法。

4.通过viewChild 获取子组件值

 

 

 父类通过viewChild 获取到子组件值

5.通过实现ControlValueAccessor接口实现自定义组件(有空再写)

6.通过服务实现数据共享和组件通信

 child1去订阅服务中的obs

 child2注册为public service,并去掉onpush策略(使用了onpush策略,不触发detectChanges()方法,视图默认无需更新,去掉onpush策略时,angular通过ngzone补获到异步事件,会自动触发detectchanges方法,以此更新视图)

 父组件input框,ngModelChange绑定changeService方法

 通过setObs去传递值给child1通过改变showText去改变child2展示的值

 

标签:my,component,入土,ChangeDetectionStrategy,组件,第二节,com,angular
From: https://www.cnblogs.com/lslhhh/p/17324334.html

相关文章

  • Angular 复习与进阶系列 – Component 组件 の Lifecycle Hooks
    前言我们在这篇和 这篇中已经学习了几个基本的LifecycleHooks.分别是constructorOnInitAfterContentInitAfterViewInitOnDestroyOnChanges这篇我们会把其余的LifecycleHooks都学完. InitGroupandChangesGroupAngular的Lifecycle可以分为两组.第一组......
  • AngularJS 输入验证
      属性描述$dirty表单有填写记录$valid字段内容合法的$invalid字段内容是非法的$pristine表单没有填写记录......
  • AngularJS 动画
    ngAnimate做了什么?ngAnimate模型可以添加或移除class。ngAnimate模型并不能使HTML元素产生动画,但是ngAnimate会监测事件,类似隐藏显示HTML元素,如果事件发生ngAnimate就会使用预定义的class来设置HTML元素的动画。AngularJS添加/移除class的指令:ng-s......
  • 关于 Angular 12 的 inlineCriticalCss 选项
    inlineCriticalCss是Angular项目中的一个配置选项,用于指定是否将关键CSS内联到页面HTML中。通常情况下,网页中的CSS文件是由浏览器异步加载的,这意味着在浏览器加载完HTML后还需要额外的时间来加载CSS文件,这会导致页面的首次渲染时间较长,用户体验不佳。为了解决这个问......
  • Angular 复习与进阶系列 – Angular Compiler (AKA ngc)
    前言在GetStarted那一篇,我们有提到过 AngularCompilation.这篇稍微给点具体画面,让大家感受一下.但不会讲细节,对细节感兴趣的可以看Medium– HowtheAngularCompilerWorks ......
  • [Angular v16] Signals
    Service:fromObservable&fromSignalcantransformobservabletoandfromsignals.import{HttpClient,HttpErrorResponse}from'@angular/common/http';import{Injectable,signal}from'@angular/core';import{catchError,fi......
  • 第二节:jsx语法深度剖析和jsx本质的探究
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblogs.com/yaopengfei/声     明1:如有错误,欢迎讨论,请勿谩骂^_^。声     明2:原创博客请在转载......
  • Angular + quill实现富文本编辑器
    前言由于需要一个富文本编辑器来编辑一些网页内容,手动编辑后存储到数据库比较麻烦,所以着手实现一个自己的富文本编辑器,来编辑和存储一些html文件.这里使用Angular框架,加Quill库实现.ngx-quill:https://github.com/KillerCodeMonkey/ngx-quillquill官网:https://quil......
  • Angular 复习与进阶系列 – Naming Conversion
    前言命名规范对项目维护是很重要的.Angular对项目的渗透很大的,必须做好命名规范,不然会很乱. Angular NamingConversionInjectionToken=UPPER_SNAKE_CAREconstSERVICE_CONFIG_TOKEN=newInjectionToken('ServiceConfig'); elementattributeandproperty......
  • angular项目启动报Another process, with id 24289, is currently running ngcc.
    在npmbuild时突然停下来,再启动就启不起来了。看报错信息是端口被占用,在任务管理器中也找不到这个端口重启vscode、重启电脑都不好使。。可以通过删除node_modules再重新npminstall解决! ......