绑定数据_【Angular】组件的数据绑定、事件绑定和属性绑定

60f7fdb8197a00e8000ad141840b9b35.gif

组件的数据绑定

Angular 支持双向数据绑定,Angular 中使用{{}}绑定业务逻辑里面定义的数据。

在app.component.html中:

<h1>
   
  {{ msg }}
 
h1>

app.component.ts中:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  msg = '欢迎来到码上加油站';
}

运行项目:

0c17526f5ad8ad0eb0fb85b61cf23e9e.png

组件的事件绑定

在app.component.html中:

<h1>
   
  {{ msg }}
  <br/>
  <button (click)="run()">执行事件button>
h1>

<router-outlet>router-outlet>

在app.component.ts中:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  msg = '欢迎来到码上加油站';
  run(){
    alert('这是一个自定义事件方法')
  }
}

运行项目后,点击按钮:

3468c8250c82bb36717808bbba6e7984.png

组件绑定属性

在app.component.html中:

<h1>
   
  {{ msg }}
  <br/>

  <button (click)="run()">执行事件button>

  <div [title] = "msg"> 鼠标放在我身上看看效果div>
h1>

<router-outlet>router-outlet>

在app.component.ts中:

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  msg = '欢迎来到码上加油站';
  run(){
    alert('这是一个自定义事件方法')
  }
}

运行项目,鼠标移动看效果:

102cb2e2c9ebe5ee41a44a87e07261f3.png

  码上加油站

  一起来加油

长按扫码关注

8d250eaf771868c489fa098b88802953.png 记得点个 在看 哦!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值