angular4-自定义组件
在 Angular 中,我们可以使用 {{}} 插值语法实现数据绑定。
新建组件 $ ng generate component simple-form --inline-template --inline-style
# Or
$ ng g c simple-form -it -is # 表示新建组件,该组件使用内联模板和内联样式
//会自动为simple-form生成simple-form.component.ts文件,文件中的selector为:app-simple-form,自动添加了app-前缀
输出:
installing component
create src/app/simple-form/simple-form.component.spec.ts // 用于单元测试
create src/app/simple-form/simple-form.component.ts // 新建的组件
update src/app/app.module.ts //Angular CLI 会自动更新 app.module.ts 文件。把新建的组件添加到 NgModule 的 declarations 数组中
app.module.ts更新后:
@NgModule({
declarations: [
AppComponent,
SimpleFormComponent
],
...
})
export class AppModule { }
创建 UserComponent 组件
import { Component } from '@angular/core';
@Component({ //Component 装饰器来定义组件的元信息
selector: 'sl-user',
template: `
<h2>大家好,我是{{name}}</h2>
<p>我来自<strong>{{address.province}}</strong>省,
<strong>{{address.city}}</strong>市
</p>
<p>{{address | json}}</p>//Angular 内置的 json 管道,来显示对象信息
`, })
//定义组件类
export class UserComponent {
name = 'name';
address = { province: 'province', city: 'city' }
} //使用构造函数初始化数据
export class UserComponent {
name: string;
address: any;
constructor() {
this.name = 'name';
this.address = {
province: 'province',
city: 'city'
}
}
}
//接口使用
interface Address {
province: string;
city: string;
}
export class UserComponent {
name: string;
address: Address;
constructor(){
this.name = 'name';
this.address = {
province: 'province',
city: 'city'
}
}
}
定义数据接口( TypeScript 中的接口是一个非常灵活的概念,除了可用于对类的一部分行为进行抽象以外,也常用于对「对象的形状(Shape)」进行描述。)
interface Person {
name: string;
age: number;
}
let semlinker: Person = {
name: 'semlinker',
age:
};
声明 UserComponent 组件
// ...
import { UserComponent } from './user.component';//载入
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, UserComponent],//声明
bootstrap: [ AppComponent ]
})
export class AppModule { }
在AppComponent中使用 UserComponent 组件
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<sl-user></sl-user> //UserComponent 的 selector
`,
})
export class AppComponent {}
angular4-自定义组件的更多相关文章
- angular4自定义组件非input元素实现ngModel双向数据绑定
在angular里我们一般都是给input元素添加[(ngModel)]="value"实现数据双向绑定,如果想实现自定义的组件上实现ngModel双向数据绑定应该怎么办呐... ...
- ionic3+angular4开发混合app 之自定义组件
这里主要是记录ionic3+angular4开发混合app时自定义组件,我想自定义组件的方法和angular4应该类似,具体在纯angular4中自定义组件,暂时没有实践,个人觉得差别不大,之后实践了 ...
- ionic3.x angular4.x ng4.x 自定义组件component双向绑定之自定义计数器
本文主要示例在ionic3.x环境下实现一个自定义计数器,实现后最终效果如图: 1.使用命令创建一个component ionic g component CounterInput 类似的命令还有: ...
- Android开发之自定义组件和接口回调
说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...
- Android自定义组件
[参考的原文地址] http://blog.csdn.net/l1028386804/article/details/47101387效果图: 实现方式: 一:自定义一个含有EditText和Butt ...
- 自己写的几个android自定义组件
http://www.see-source.com/androidwidget/list.html 多多指点,尤其是自定义组件的适配问题,希望能有更好的方法
- PhoneGap: Android 自定义组件
Hello Core Demo Plugin Development(组件部署): http://docs.phonegap.com/en/2.0.0/guide_plugin-development ...
- android开发之自定义组件
android开发之自定义组件 一:自定义组件: 我认为,自定义组件就是android给我们提供的的一个空白的可以编辑的图片,它帮助我们实现的我们想要的界面,也就是通过自定义组件我们可以把我们要登入的 ...
- HTML5 UI框架Kendo UI Web教程:创建自定义组件(三)
Kendo UI Web包 含数百个创建HTML5 web app的必备元素,包括UI组件.数据源.验证.一个MVVM框架.主题.模板等.在前面的2篇文章<HTML5 Web app开发工具Ke ...
- HTML5 UI框架Kendo UI Web中如何创建自定义组件(二)
在前面的文章<HTML5 UI框架Kendo UI Web自定义组件(一)>中,对在Kendo UI Web中如何创建自定义组件作出了一些基础讲解,下面将继续前面的内容. 使用一个数据源 ...
随机推荐
- Python量化库大全
https://zhuanlan.zhihu.com/p/26983703?utm_source=wechat_session&utm_medium=social 这个网址上详细介绍了,做量化 ...
- (转)UCOSII在任务切换与出入中断时堆栈指针的使用
1 uc/os ii在M3中的堆栈结构 1.1 M3入账序列 1.2 加上手工入栈序列 2 PendSV在Cortex-M3中的应用 Systick为嵌入到内核中,优先级比一般中断优先级高.若在一 ...
- if标签
If标签如果php中if语句的作用,if是用于流程控制的. 在ThinkPHP中if标签也是用于流程控制的. If标签的语法格式: <if condition=’条件表达式’> 输出结果1 ...
- 从华为数字化转型看IT自动化运维重要性
关注嘉为科技,获取运维新知 8月有幸聆听了华为CIO陶总(陶景文)关于“华为数字化转型秘诀:坚定“以客户为中心”的思想”的演讲,获益良多.其中陶总也分享了IT运营平台的建设对于华为实现数字化转型的重要 ...
- Confluence 6 让一个空间可以公众访问
如果你希望将一个空间分享给没有登录 Confluence 的用户(匿名用户)可以访问的话,你需要将这个空间标记为 公开(public). 让一个空间可以公开的访问的话,你就不能选择如何对这个空间进行 ...
- linux导出sql数据
1. 导出数据库的数据 在linux命令行下输入 mysqldump -u userName -p dabaseName > fileName.sql 在linux命令行下输入 2. 导出表 ...
- 『TensorFlow』第九弹_图像预处理_不爱红妆爱武装
部分代码单独测试: 这里实践了图像大小调整的代码,值得注意的是格式问题: 输入输出图像时一定要使用uint8编码, 但是数据处理过程中TF会自动把编码方式调整为float32,所以输入时没问题,输出时 ...
- navicat 连接 mysql 解决出现client does not support authentication protocol requested by server的问题
MySQL8换了加密插件,数据库管理客户端都来不及更新,连接方式缺乏sha2的加密方式首先第一步, UPDATE mysql.user SET plugin = 'mysql_native_passw ...
- ECharts 报表事件联动系列四:柱状图,折线图,饼状图实现联动
代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- Oracle 12c新特性之——TABLE ACCESS BY INDEX ROWID BATCHED
Oracle12c开始,我们在获取SQL语句的执行计划时,也会经常看到"TABLE ACCESS BY INDEX ROWID BATCHED"操作,那么,这个操作到底是什么意思呢 ...