nvm install stable #安裝最新版node nvm install 8.4.0 # 安裝8.4.0版本的node nvm ls #列出可以使用的node nvm use 8.4.0 #使用8.4.0版本的node;
npm configset registry https://registry.npm.taobao.org
npm install -g typescript typings
npm install -g angular-cli@latest //若是用bash安裝失敗的話,能夠在桌面使用命令行工具cmd,來安裝
ng new angulardemo
npm run start
npm install -g angular-cli/@1.0.0
https://www.angular.cn/
jquery: npm install @types/jquery --save-devcss
bootstrap: npm install @types/bootstrap --save-devhtml
{ path : "home", component: HomeComponent, children: [ {path: "list", component: HomeList}, {path: "create", component: HomeCreate}, ] }
主路由在跳路由時,路徑是直接寫的<a [routerLink]="['/product']">詳情</a>
node
若是是子路由在同級之間跳轉,路徑是以./開頭的,好比product下面有create和list,路徑不用寫product/list,而是./list
<a [routerLink]="['./list']">詳情</a>
jquery
路由的五個對象typescript
這裏面的路由,配置的是路徑和組件的對應關係,而不是和模塊的對應
path:"user" (path不能以/開頭) 若是path: "**",通配符匹配,必須放在最後面
compoent:"A"npm
<a [routerLink]="['/']">主頁</a> <a [routerLink]="['/product']">詳情</a> <router-outlet></router-outlet> [routerLink] 的[]表示屬性綁定 <button (click)="handler()"><button>事件綁定
在 app.components.ts文件中的組件類函數中添加json
constructor(private router: Router) {}//導入router對象 handler(){ this.router.navigate(["/product"]) } //設置handler函數,路由跳轉
/product?id=1&name=2 => ActivatedRoute.queryParams[id]bootstrap
<a [routerLink] = "['/product']" [queryParams]="{id: 1}"></a> export class ProductComponent implements Oninit { private productId: number; construct (private routeInfo: ActivatedRoute){} ngOnInit(){ this.productId = this.routeInfo.snapshot.queryParams["id"]; } }
{path: /product/:id} => /product/1 => ActivatedRoute.params[id]數組
<a [routerLink] = "['/product', 1]"></a> this.id = this.routeInfo.params["_value"]["id"];
//在組件類中的構造函數中,注入服務; constructor(private productService: ProductService){}
//在 providers: [{provide: ProductService, useClass: ProductService}] // 能夠簡寫成 providers: [ProductService] //useClass實例化方式,new誰. provide是token,就是在構造函數中聲明的ProductService1;
ng g service shared/product
在shared文件夾中建立一個product的服務;bash
@ngModule({providers:[productService]})
@Component({providers: [{provide: ProductService, useClass: AnotherProductService}]})
export class productComponent implements Oninit{ product: product; //聲明變量 //在構造函數中注入服務 constructor(private productService: productService){} ngOnInit(){ this.product = this.productService.getProduct(); } }
angular4的屬性綁定
<img [src]="imgUrl">
用[]把src屬性包起來,[src]就是angular4的屬性綁定,能夠直接使用在此組件中定義的imgUrl的值;
css類屬性綁定
<span *ngFor="let star of stars" class="btn" [class.primite] = "star">
[class.promite] = "star"是說span標籤有沒有css類promite,取決於star是否是true。和ng-class相似;
用[]把組件中的數據,綁定到模板上
export class BindComponent implements OnInit{ name: string; //值 constructor(){} ngOnInit() } <input [value]="name"> //從組件到模板
用()把組件中的事件綁定到組件中的方法中
<input (input)="doOnInput($event)"> export class BindComponent implements OnInit{ constructor(){} ngOnInit() doOnInput(event) {} } //單項綁定,從模板到控制器
<input [(ngModel)]="name"> export class BindComponent implements OnInit{ name: string; constructor(){} ngOnInit() doOnInput(event) {} }
在標籤中用[(ngModel)],在組件類中直接定義:name: string;不在contructor中,也不在哪一個鉤子中;
在裝飾器中導入FormsModule
@NgModule({ imports: [ FormsModule, HttpModule, ... ] })
模板式表單只能用指令來定義數據模型,如下三個指令都來自FormsModule
<input type="checkbox" ngModel #isHost="ngModel" name="isHost"> {{isHost.value}} <div class="form-group" *ngIf="isHost.value"> </div>
在input標籤中添加ngModel和name屬性,把該標籤的數據添加到表單數據模型中
用#isHost="ngModel" 把標籤的值暴露給頁面模板,若是沒有這個屬性就沒辦法在html頁面上用{{isHost}}顯示出來;
在裝飾器中導入ReactiveFormsModule
@NgModule({ imports: [ ReactiveFormsModule, HttpModule, ... ] })
建立響應式表單須要兩步
數據模型由FormControl, FormGroup,FormArray這三個類型組成;
export class CdAppPublishComponent implements OnInit { username: FormControl = new FormControl("aaa") //新建一個表單元素,初始值爲"aaa" formModel: FormGroup = new FormGroup({ //新建一個FormGroup form: new FromControl(), to: new FromControl(), }) emails: FormArray = new FormArray([ new FormControl("a@a.com"), ]) }
ng g component navbar
建立一個navbar導航組件;會在app文件夾中直接建立一個navbar文件夾,裏面是相應的文件;
建立後會自動注入到appModules父組件中
把父組件中的數據傳遞給子組件;
<app-stars [rating] = "product.rating">
app-stars這個組件的rating屬性是由父組件的product.rating傳遞來的
在app-stars組件的構造函數的鉤子OnInit中設置
@Input
private rating: number = 0;
rating變量是number類型,初始化值是0,私有數據,
由父組件傳入(@Input輸入裝飾器);
也就是說裝飾器是直接寫在一個變量,構造函數的上面起做用的
輸入屬性是指被input裝飾器註解的屬性;用來從父組件接受數據.屬性綁定是單向的,從父組件到子組件
在子組件中
// import { Component,ngOnInit, Input } from "@angular/forms"; //引入Input裝飾器,沒錯裝飾器也須要引入; export class OrderComponent implements OnInit{ //用輸入裝飾器修飾這兩個變量,表示這兩個值是從父組件傳入的; @Input() storkCode: string; @Input() amount: number; constructor(){}; ngOnInit(){} }
<div>買{{amount}}個 {{storkCode}}</div>
在父組件中
export class AppComponent { stock = ""; }
<div>我是父組件</div> <input type="text" placeholder= "請輸入股票代碼" [(ngModel)]= "stock" > <app-order [stockCode]="stock" [amount]="100"></app-order>
@Input() storkCode: string;
constructor(routeInfo: ActivatedRoute) { } ngOnInit(){ this.productId = this.routeInfo.snapshot.queryParams["id"]; }
//js運行環境,引入要使用的組件(Component),鉤子(ngOnInit),裝飾器(Input) import { Component,ngOnInit, Input } from "@angular/forms"; /* 1. export導出,使外部可使用這個對象 2. class表示這是一個對象的類,裏面的屬性(好比title)都是this.title. 裏面的函數好比console(){console.log("123")}都是this.console; 而上面的this(實際上並不存在)指的是用這個組件類實例化出來的組件 3. 在export class OrderComponent implements OnInit{}是一個對象,不是函數。裏面不能直接運行js. 要運行js的話應該在constructor(){console.log("這裏面")}或者是ngOnInit(){}裏面; */ export class OrderComponent implements OnInit{ //這裏面定義的變量,函數,都是能夠爆露在組件實例的模板上的值;constructor是構造函數。 //ngOnInit是當初始化是運行的函數鉤子; //用輸入裝飾器修飾這兩個變量,表示這兩個值是從父組件傳入的; @Input() storkCode: string; @Input() amount: number; constructor(){}; ngOnInit(){} }
組件中的OnChanges函數,能監控自身組件的值的變化,也能監控父組件傳入的值的變化。
要被OnChanges函數監控,要知足下列條件
export class ChildComponent implements OnInit, OnChanges { greeting: string; user: {name: string}; constructor(){} //changes是ngOnChanges的參數,監控到的變化信息存儲在裏面。SimpleChanges是 //changes的數據類型; ngOnChanges(changes: SimpleChanges): void { console.log(changes); } //changes的數據模型以下所示 { "greeting": { "previousValue": {}, "currentValue": "Hello" } } }
ngDoCheck(): void { if(this.user.name !== this.oldUser.name) { console.log("改變了「) } }
message: string; ngAterViewInit(): void { setTimeout(() => { this.message = "hello"; }, 0) }
export class ChildComponent{ greeting(name: string) { console.log(name) } }
<app-child #child1></app-child> <app-child #child2></app-child>
export class ChildComponent{ @ViewChild("child1") child1: ChildComponent; ngOnInit(): void { this.child1.greeting("tom") } }
<div> 我是子組件 <ng-content select=".header"><ng-content> <ng-content select=".footer"><ng-content> </div>
<div> 我是父組件 <app-child> <div class="header">這是頁頭,這個div時父組件投影到子組件中的內容,title: {{title}}</div> <div class="footer">這是頁腳,這個div時父組件投影到子組件中的內容</div> </app-child> </div> <div [innerHTML] = 'divContent'> </div>
export class AppComponent { title = "app works"; divContent = "<div>慕課網;</div>" }