惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

zodream梦想开源/个人编程日记

文件解析笔记-zodream梦想开源/个人编程日记 密码本开发笔记之读写与保存-zodream梦想开源/个人编程日记 SkiaSharp 把 pixel byte[] 转成 SKBitmap-zodream梦想开源/个人编程日记 nas 使用 Docker 安装 gogs-zodream梦想开源/个人编程日记 复制 android 手机中的文件到电脑-zodream梦想开源/个人编程日记 周报:寻找优质的周刊-zodream梦想开源/个人编程日记 开发日志:对Markdown的代码块新增引用来源支持-zodream梦想开源/个人编程日记 周报:怎么写技术类的教程文章-zodream梦想开源/个人编程日记 css display:flex 布局尺寸超出问题-zodream梦想开源/个人编程日记 周报:SEO优化的思考-zodream梦想开源/个人编程日记 Edge 浏览器不适用 Edge Image Viewer 打开图片 -zodream梦想开源/个人编程日记 SEO 学习笔记(一) 内容来源-zodream梦想开源/个人编程日记 PHP 实现双因素身份认证(2FA)-zodream梦想开源/个人编程日记 WPF MVVM 获取List 多选数据-zodream梦想开源/个人编程日记 Burp Suite 抓包-zodream梦想开源/个人编程日记 使用 indexnow 注意事项-zodream梦想开源/个人编程日记 Godot 使用字体图标 例如: Iconfont、FontAwesome-zodream梦想开源/个人编程日记 angular 15 对指定页面进行访问限制-zodream梦想开源/个人编程日记 CSS 使用 column-count 实现瀑布流出现内容分割的解决办法-zodream梦想开源/个人编程日记 input 确认按键事件在手机端不生效-zodream梦想开源/个人编程日记 C# 使用socket 进行通讯-zodream梦想开源/个人编程日记 Maui开发中Windows应用开启管理员权限-zodream梦想开源/个人编程日记 Maui 中自定义控件-zodream梦想开源/个人编程日记 angular 14 使用 ng-template 实现tree 结构显示-zodream梦想开源/个人编程日记 c# 动态安装和卸载dll-zodream梦想开源/个人编程日记 慎用 CompositionTarget.Rendering-zodream梦想开源/个人编程日记 c# 重写 c++ 程序笔记:数据初始化-zodream梦想开源/个人编程日记 源码编译 aseprite-zodream梦想开源/个人编程日记 记录一下字符串分隔split各语言之间的不同-zodream梦想开源/个人编程日记 c# Gzip解码无头内容-zodream梦想开源/个人编程日记
angular9教程之自定义部件及获取页面元素-zodream梦想开源/个...
zodream · 2020-05-27 · via zodream梦想开源/个人编程日记

组件接受传值

使用 @Input() 定义

@Component({
    selector: 'app-page-tip',
    template: './page-tip.component.html',
    styleUrls: ['./page-tip.component.scss']
})
export class PageTipComponent {

    @Input() public title = '提示';

}

12345678910

使用

<app-page-tip [title]="'标题'"></app-page-tip>

1

组件事件通知

通过 @Output() 定义事件,使用 .emit() 触发事件通知

@Output() public valueChange = new EventEmitter();

public tap() {
    this.valueChange.emit();
}

12345

使用

<app-page-tip (valueChange)="tapValue()"></app-page-tip>

1

双向绑定

使用 [()] 进行双向绑定,那么应该修改事件,保证 事件名为 参数名 + Change

例如

@Component({
  selector: 'app-page-tip',
  template: './page-tip.component.html',
  styleUrls: ['./page-tip.component.scss']
})
export class PageTipComponent {

    @Input() public title = '提示';


    @Output() public titleChange = new EventEmitter();

    public tap() {
        this.titleChange.emit();
    }
}

12345678910111213141516

使用

<app-page-tip [(title)]="title"></app-page-tip>

1

获取页面元素

例如

<div>12312</div>
<app-page-tip [(title)]="title"></app-page-tip>

12

获取 div,必须在div 上加 #+命名

然后通过 @ViewChild('命名') 获取

    @ViewChild('app')
    private box: ElementRef;

12

这是 box.nativeElement 就是 HTMLDivElement,可以想普通元素一样操作。

如果要绑定事件,那么请注意先等页面已经生成,不然获取不到。

ngAfterViewInit 就是在页面初始化后触发。

export class HomeComponent implements AfterViewInit {

  @ViewChild('app')
  private box: ElementRef;

  ngAfterViewInit(): void {
    const div = this.box.nativeElement as HTMLDivElement;
    div.addEventListener('click', (event) => {
        // TODO
    });
  }
}

123456789101112

获取页面上的自定义组件

例如

<app-page-tip [(title)]="title"></app-page-tip>

1

通过 @ViewChild(组件类名) 获取

    @ViewChild(PageTipComponent)
    private box: PageTipComponent;

12

转载请保留原文链接: https://zodream.cn/blog/id/149.html