Angular2之入门示例
概述在学ng2,手写一个例子感受下,当然是经典的双向数据绑定. 环境“@angular/core”: “^4.0.0” + Typescript 2.3.4 代码展示文件组织src/app 目录下主要文件: 首先是根模块 import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { TwowayBindComponent } from './twoway-bind/twoway-bind.component';
@NgModule({
declarations: [
AppComponent,HelloWorldComponent,UserItemComponent,UserListComponent,TwowayBindComponent
],imports: [
BrowserModule,FormsModule // 记得写上
],providers: [],bootstrap: [AppComponent]
})
export class AppModule { }
再就是根组件 import { Component } from '@angular/core';
@Component({
selector: 'app-root',templateUrl: './app.component.html'
})
export class AppComponent {
}
双向绑定的实现 import { Component,OnInit } from '@angular/core';
@Component({
selector: 'app-twoway-bind',template: `
<div>
<input type="text" [(ngModel)]="username">
<p>{{ username }}</p>
</div>
`
})
export class TwowayBindComponent implements OnInit {
username: string = 'Hello World!';
ngOnInit(): void {
}
}
注意上面的 最后就是在页面上调用这个组件,在 <app-twoway-bind></app-twoway-bind>
欢迎补充指正! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |