国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB開發(fā) > 正文

ng1和ng2的部分對比

2024-04-27 15:07:33
字體:
供稿:網(wǎng)友

前言:

  angular2相比angular1做了革命性的改變。對于開發(fā)者來說,我們知道它框架的實現(xiàn)上改變極大。我們看到代碼也能發(fā)現(xiàn)它寫法上變化很大,似乎完全是另一個東西。

但是當(dāng)我們真正去寫下去的時候,又會發(fā)現(xiàn),處處都有angular1的影子,處處都是angular1的概念。對,沒錯。angular始終是angular,換件“衣服”,還是angular。

最開始我第一眼看到angular2的代碼的時候,是有點排斥的,怎么感覺就像是react的寫法...而后,我自己親手,寫它的demo時候發(fā)現(xiàn),這貨還是原本的angular,一切都那么熟悉。

所以有angular1基礎(chǔ)的同僚,完全不用排斥。下面來對比一部分兩個版本的寫法。

 

directive

 

angular1

angular2

ng-app

Bootstrapping

<body ng-app="myapp">

ng1中初始化引導(dǎo)應(yīng)用,通過ngApp屬性定義應(yīng)用,并通過定義ng-view屬性渲染到相應(yīng)dom

import { bootstrap } from '@angular/platform-browser-dynamic';import { AppComponent } from './app.component';bootstrap(AppComponent);

ng2引導(dǎo)應(yīng)用通過bootstrap類實例化,AppComponent的@Component的selector屬性選擇dom進行渲染

 

ng-class

ngClass

 
<div ng-class="{active: isActive}"><div ng-class="{active: isActive,shazam: isImportant}"><div ng-class="{true: 'active',false: 'isImportant'}[isActive]

 

 
<div [ngClass]="{active: isActive}"><div [ngClass]="{active: isActive,shazam: isImportant}"><div [class.active]="isActive">

 [class.active]指代的就是active類,最開始我一看到還以為是偽類的寫法

ng-click

click event

 
<button ng-click="vm.toggleImage()"><button ng-click="vm.toggleImage($event)">

 

 
<button (click)="toggleImage()"><button (click)="toggleImage($event)">

 

ng-controller

Component decorator

 
<div ng-controller="MovieListCtrl as vm">

 

 復(fù)制代碼
@Component({  selector: 'movie-list',  templateUrl:'app/movie-list.component.html',  styleUrls: ['app/movie-list.component.CSS'],  pipes: [StringSafeDatePipe]})復(fù)制代碼

 

ng-show or ng-hide

[hidden] 

 
<h3 ng-show="vm.favoriteHero">  Your favorite hero is: {{vm.favoriteHero}}</h3>

 

 
<h3 [hidden]="!favoriteHero">  Your favorite hero is: {{favoriteHero}}</h3>

只是用[hidden]屬性,沒有[show]屬性

ng-href

[href]

 
<a ng-href="angularDocsUrl">Angular Docs</a>

 

 復(fù)制代碼
@RouteConfig([    {        path: '/movies',        name: 'Movies',        component: HeroesComponent    }])復(fù)制代碼
<a [href]="movies">Angular Docs</a>
<a [routerLink]="['Movies']">Movies</a>

[href] 對應(yīng)的是路由配置里path鏈接, [routerLink] 對應(yīng)的是路由name 。

ng-if

*ngIf

 
<table ng-if="movies.length">

 

 
<table *ngIf="movies.length">

 

ng-model

ngModel

 
<input ng-model="vm.favoriteHero"/>

ng-model在angular1中是雙向綁定指令

 
<input [(ngModel)]="favoriteHero" />
<input bindon-ngModel="favoriteHero">

[()]在angular2中代表雙向綁定, 也可以使用bindon-ngModel,推薦前者寫法

ng-repeat

*ngFor

<tr ng-repeat="movie in vm.movies">

 

<tr *ngFor="let movie of vm.movies">

如果不加*,只會遍歷一個項

ng-src

[src]

 
<img ng-src="{{movie.imageurl}}">

 

 
<img [src]="movie.imageurl">

 

ng-style

ngStyle

 
<div ng-style="{color: colorPReference}">

 

 
<div [ngStyle]="{color: colorPreference}"><div [style.color]="colorPreference">

 

ng-switch

ngSwitch

 復(fù)制代碼
<div ng-switch="vm.favoriteHero">    <div ng-switch-when="true">      Excellent choice!    </div>    <div ng-switch-when="false">      No movie, sorry!    </div>    <div ng-switch-default>      Please enter your favorite hero.    </div></div>復(fù)制代碼

 

 復(fù)制代碼
<span [ngSwitch]="favoriteHero">  <p *ngSwitchWhen="true">    Excellent choice!  </p>  <p *ngSwitchWhen="false">    No movie, sorry!  </p>  <p *ngSwitchDefault>    Please enter your favorite hero.  </p></span>復(fù)制代碼

 

 

Filters / Pipes:

 

 

angular1

angular2

currency

currency

<td>{{movie.price | currency}}</td>

 

<td>{{133567 | currency:'USD':true}}</td> //$133,567

<td>{{133567 | currency:'RMB':true}}</td> //RMB133,567

屬性值不支持¥,$等符號,必須按照USD,RMB這樣寫,否則不顯示

 

date

date

 
<td>{{movie.releaseDate  | date}}</td>

 

 
<td>{{movie.releaseDate | date}}</td>

 

filter

none

 
<tr ng-repeat="movie in movieList | filter: {title:listFilter}">

 

 

由于性能原因,ng2沒有filter指令,需要在component用戶自己定義過濾

json

json

 
<pre>{{movie | json}}</pre>

 

 
<pre>{{movie | json}}</pre>

和 JSON.stringify 功能相同 ,和 angular1 的 json 一樣

limitTo

slice

 
<tr ng-repeat="movie in movieList | limitTo:2:0">

 

 
<tr *ngFor="let movie of movies | slice:0:2">

 

lowercase

lowercase

 
<div>{{movie.title | lowercase}}</div>

 

 
<td>{{movie.title | lowercase}}</td>

 

number

number

 
<td>{{movie.starRating  | number}}</td>

 

 
<td>{{movie.starRating | number}}</td><td>{{movie.starRating | number:'1.1-2'}}</td><td>{{movie.approvalRating | percent: '1.0-2'}}</td><td>{{movie.approvalRating | percent:'4.3-5'}}</td>

number 和 percent 屬性值控制小數(shù)點后面的位數(shù),只是寫法讓人看不懂,有誰知道為什么是這樣?

orderBy

none

 
<tr ng-repeat="movie in movieList | orderBy : 'title'">

 

 

也是由于性能問題,ng2不再提供此指令

 

Controllers / Components:

   angular1 視圖的模型和方法都在控制器(Controllers)里,angular2中建立這些在組件(Components)里。

 

angular1

angular2

currency

currency

<td>{{movie.price | currency}}</td>

 

<td>{{133567 | currency:'USD':true}}</td> //$133,567

<td>{{133567 | currency:'RMB':true}}</td> //RMB133,567

屬性值不支持¥,$等符號,必須按照USD,RMB這樣寫,否則不顯示

 

IIFE(函數(shù)表達式)

none

  

在angular1中,我們經(jīng)常定義一個立即調(diào)用函數(shù)表達式(或IIFE)在我們的控制器代碼。

這讓我們的控制器代碼全局命名空間。

  

angular2中我們不需要擔(dān)心這個問題, 因為我們使用ES 2015模塊和模塊處理我們的命名空間

Angular modules

import

 
angular.module("movieHunter", ["ngRoute"]);

 

 
import { Component } from '@angular/core';import { ROUTER_DIRECTIVES } from '@angular/router-deprecated';

angular2 使用es2015 modules ,每個代碼文件都是模塊,可以直接導(dǎo)入文件模塊使用

Controller registration

Component Decorator

 
angular  .module("movieHunter")  .controller("MovieListCtrl",              ["movieService",               MovieListCtrl]);

在angular1中,注冊模塊下的控制器,通過以上方法。

第一個參數(shù)是控制器命名,第二個參數(shù)用字符串定義所有依賴,和一個控制器引用函數(shù)

 復(fù)制代碼
@Component({  selector: 'movie-list',  templateUrl:'app/movie-list.component.html',  styleUrls: ['app/movie-list.component.css'],  pipes: [StringSafeDatePipe]})復(fù)制代碼

 angular2中,我們通過@Component提供元數(shù)據(jù),如模板和樣式

Controller function

Component class

 
function MovieListCtrl(movieService) {}

在angular1中,我們編寫模型和方法在控制器函數(shù)里。

 
export class MovieListComponent {}

在angular2中,我們創(chuàng)建一個組件類編寫模型和方法

Dependency Injection

Dependency Injection

 
MovieListCtrl.$inject = ['MovieService'];function MovieListCtrl(movieService) {}

ng1中,必須要對每個依賴進行注入

 
constructor(movieService: MovieService) {}

在ng2中,constructor注入依賴,但是需要模塊被當(dāng)前組件或者當(dāng)前組件的父組件引入依賴。

比如,當(dāng)前組件引入依賴服務(wù), import { MovieService } from './MovieService';

 

Style Sheets:

 

angular1

angular2

link tag

link tag

<link href="styles.css" rel="stylesheet" />

<link href="styles.css" rel="stylesheet" />

屬性值不支持¥,$等符號,必須按照USD,RMB這樣寫,否則不顯示

 

StyleUrls

angular2 中 我們可以在@Component 中引入css,

此css默認會在當(dāng)前組件形成一個獨立的css作用域。

詳情可以看此系列第三篇博客。“英雄之旅”見聞和小結(jié)----angular2系列(三)

styleUrls: ['app/movie-list.component.css'],

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 越西县| 东光县| 彝良县| 江阴市| 甘肃省| 大兴区| 闵行区| 廊坊市| 上犹县| 双峰县| 江都市| 旬邑县| 焦作市| 金乡县| 普兰县| 彭州市| 贡觉县| 三明市| 永寿县| 抚远县| 彰化市| 洪雅县| 衡东县| 邯郸县| 澄迈县| 昔阳县| 柳河县| 桐柏县| 乐东| 祁门县| 长兴县| 鄂尔多斯市| 雅安市| 诏安县| 澄城县| 航空| 清新县| 宁河县| 十堰市| 岗巴县| 巴南区|