angular-2

2020-04-29  本文已影响0人  南崽

angular基础模板语法

文本绑定

html绑定

属性绑定

class绑定

style绑定

src绑定

条件渲染

列表渲染

表单双向绑定

<input [value]="msg" (input)="msg=$event.target.value">
import {FormsModule} from '@angular/forms';

// 导入表单模块
<input [(ngModel)]="msg">

事件

管道

引用

组件

创建组件

ng g c 文件夹/组件名称
ng g c components/child

组件

@Component({
  selector: 'app-child',
  templateUrl: './child.component.html',
  styleUrls: ['./child.component.css']
})
export class ChildComponent implements OnInit {

}

组件参数的传递

 <app-child [list]="list"></app-child>
 @Input() list:string[];
 @Output() selNum=new EventEmitter();
selNum.emit("数据")
<app-child [list]="list" (selNum)="setType($event)"></app-child>
<app-child #child2>
{{child2.list}}

childlist数据必须是public

Ref

<p #refp></p>

 import { Component, ViewChild, ElementRef,QueryList,ViewChildren } from '@angular/core';
// 导入组件
@ViewChild("refp",{static:true}) refp:ElementRef;
// 获取操作dom
ngAfterViewInit(){
    this.refp.nativeElement //真正的dom节点 
}
<div *ngFor="#myitem"></div>

@ViewChildren("myitem") myitem:QueryList<ElementRef>;
ngAfterViewInit(){
    this.myitem.forEach(el=>{
      el.nativeElement //真正的节点
    })
  }

服务service

创建 ng g s 文件夹/服务名称

ng g s service/search

使用

import {SearchService} from "xxx";
// 导入
constructor (private SearchService:SearchService){}
// 使用
this.SearchService.xxx
上一篇 下一篇

猜你喜欢

热点阅读