通常来说,数据绑定要么是从页面流向组件中的数据,要么是从组件中的数据流向页面。下面我们来介绍在Angular 2中数据绑定的几种不同方式。 
1. 使用{{}}将组件中的数据显示在html页面上 
  实现方式:<div>{{value}}</div> 
  这样就可将组件中的value值显示在div元素上。 
2. 使用[]DOM元素的属性值设置为组件中的数据 
  实现方式:<img [src]="srcUrl"> 
  这样就可以将img标签的src属性设置为组件中的srcUrl值。但是使用[]只能绑定在DOM元素已有的属性上,例如<div [src]="srcUrl"></div>是会报错的,以为div不存在src属性。 
  但是,可以通过自定义的方式使用@Input为我们的指令添加属性,这也是实现从父子组件数据通信的一种方式,顾名思义就是@Input定义的属性值是从父组件中获取的。 
  下面我们来介绍使用@Input实现数据绑定。

//父组件
@Component({selector: 'my-app',template: `<customer-counter [counter] = "someValue"></customer-counter>`,directives:[CustomerCounterComponent]
})export class AppComponent{someValue = 3;
}//子组件
@Component({selector: 'customer-counter',template: `<span>{{counter}}</span>`
})export class CustomerCounterComponent{counterValue = 0;@Input()get counter(){return this.counterValue;}
}

  这样<customer-counter [counter] = "someValue"></customer-counter>就可以将父组件中的someValue数据绑定在子组件的counter属性上了。 
3. 事件绑定 
  事件绑定就是通过用户的交互行为来触发DOM元素的事件。 
  例如:<button (click)="onClickMe()">点我!</button> 
  当用户点击button是就会触发onClickMe()方法,这样实现的前提是button能够监听click方法。 
  当然,也可以通过@Output在我们的组件中添加自定义事件,@Output顾名思义就是在子组件中向父组件输出东西。 
  具体实现如下:

//父组件
@Component({selector: 'my-app',template: `<customer-counter (counterChange) = "changeValue($event)"></customer-counter> <div>{{someValue}}</div>`,
    directives:[CustomerCounterComponent]
})export class AppComponent{someValue = 3;changeValue(val){this.someValue = val;}
}//子组件
@Component({selector: 'customer-counter',template: `<button (click)="decrement()">-</button>`
})export class CustomerCounterComponent{counterValue = 100;@Output() counterChange = new EventEmitter();decrement() {this.counterChange.emit(-- this.counterValue );}
}

这样<customer-counter (counterChange) = "changeValue($event)"></customer-counter>当子组件中的counterChange事件被触发,就会执行父组件中的changeValue()方法,进而改变父组件中的相关数据。 
4. 双向绑定 
  上面介绍的方式{{}}和[]是将组件中的数据绑定到DOM元素上,而()是将组件中的事件绑定到组件中的方法上,那么该如何数据的双向绑定呢? 
  Angular2是通过[()]来实现的,例如<input [(ngModel)]="value">就是双向绑定input元素的值。但是需要注意的是[()]只能绑定在有输入输出功能的DOM元素上(例如:input、textare),如果绑定在div这样的元素上就会报错。 
  那么,如何通过[()]在我们自定义的指令上实现双向绑定呢?没错,就是使用@Input和@Output来实现。

//父组件
@Component({selector: 'my-app',template: `
<customer-counter [(counter)] = "someValue"></customer-counter> <p> value: {{someValue}}</p>`,directives:[CustomerCounterComponent]
})export class AppComponent{someValue = 3;
}
//子组件
@Component({selector: 'customer-counter',template: `<button (click)="decrement()">-</button> <span>{{counter}}</span>`
})export class CustomerCounterComponent{counterValue = 0;@Input()get counter(){return this.counterValue;}@Output() counterChange = new EventEmitter();set counter(val) {this.counterValue = val;this.counterChange.emit(this.counterValue);}decrement() {this.counter--;}
}

  这样<customer-counter [(counter)] = "someValue"></customer-counter>就可以将父组件中的someValue绑定到子组件的counter属性上,同时当子组件的counter属性发生改变时也更新父组件的someValue值。 
  需要注意的是,我们定义的事件监听是counterChange,而使用的确实[(counter)]。这是因为Angular 2中约定添加后缀Change,也就是[(counter)]等价于[change]和(counterChange)的组合。如果去看[(ngModel)]的实现,你也会发现它是[ngModel]和[ngModelChange]的组合。

转载于:https://www.cnblogs.com/zzy-run-92/p/9400114.html

五:Angular 数据绑定 (Data Binding)相关推荐

  1. WPF中的数据绑定Data Binding使用小结

    完整的数据绑定的语法说明可以在这里查看: http://www.nbdtech.com/Free/WpfBinding.pdf MSDN资料: Data Binding: Part 1 http:// ...

  2. XAML数据绑定(Data Binding)

    XAML数据绑定(Data Binding) Data Binding可以使得XAML标签属性的赋值更为灵活和方便.在绑定过程中,获取数据的标签成为目标标签:提供数据的标签成为源标签.在XAML中,一 ...

  3. Data Binding Library数据绑定框架

    Data Binding Library是Google在2015年IO大会上发布的一个用于实现MVVM设计模式的支持库 环境配置 在Android Studio 2.0 原生支持Data Bindin ...

  4. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  5. Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  6. WPF中的Data Binding调试指南

    点击蓝字"大白技术控"关注我哟 加个"星标★",每日良时,好文必达! WPF中的Data Binding如何Debug? 大家平时做WPF开发,相信用Visua ...

  7. Data Binding学习(一)

    1.为什么要使用Data Binding? 当我们没使用Data Binding时候布局是这样的 <LinearLayout -><TextView android:id=" ...

  8. Android Data Binding 详细介绍与使用

    构建环境 首先,确保能使用Data Binding,需要下载最新的 Support repository.否则可能报错,如图: 在模块的build.gradle文件中添加dataBinding配置 a ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

最新文章

  1. C++动态链接库dll及静态链接库lib制作及使用教程
  2. 人脸对齐--Pose-Invariant Face Alignment with a Single CNN
  3. Telnet不是内部或外部命令解决办法
  4. mysql 修改配置生效_MySQL修改my.cnf配置不生效的解决方法
  5. php控制器和路由机制,自制PHP框架之路由与控制器
  6. 文献记录(part66)--一种基于交叉熵的社区发现算法
  7. silverlight 一些写法小计
  8. OGRE源代码resource分析
  9. 删远端分支报错remote refs do not exist或git: refusing to delete the current branch解决方法
  10. 如何在 Spring 中自定义 scope
  11. Java 8 中的 java.util.Optional
  12. linux如何实现c语言程序,在Linux下如何利用C语言来实现一个Sniffer
  13. Java Web程序设计笔记 • 【第1章 Web应用程序】
  14. 基因结构图的0_TBtools | 只有序列,怎么做基因结构图?
  15. python设置Excel单元格的数据有效性
  16. Java jks转换pem_JKS转PEM,JKS转BKS
  17. Win10_11使用VMware等启动虚拟机蓝屏报错等一些列问题解决方案
  18. 形态学填充孔洞的几个问题
  19. gitee上传大小超过100M文件
  20. 多个python版本pip对应问题

热门文章

  1. 使用rancher对Docker容器服务升级
  2. OpenCV 笔记(02)— 图像显示、保存、腐蚀、模糊、canny 边缘检测(imread、imshow、namedWindow、imwrite)
  3. Redis 笔记(02)— keys 键相关命令(查询数据库key数量、判断key是否存在、指定key过期时间、查看key类型、查看key剩余秒数、选择数据库、删除key、删除数据库)
  4. 用python实现杨辉三角的几种不同方式
  5. BERT-Pytorch demo初探
  6. Http请求之优雅的RestTemplate
  7. Dorado用法与示例
  8. BERT模型的OneFlow实现
  9. 大数据调度平台Airflow(八):Airflow分布式集群搭建及测试
  10. [JavaScript] JavaScript 数组挖掘,不只是讲数组哟