詳解Angular結(jié)構(gòu)型指令模塊和樣式
一,結(jié)構(gòu)型指令
*是一個語法糖,<a *ngIf="user.login">退出</a>相當于
<ng-template [ngIf]="user.login">
<a>退出</a>
</ng-template>
避免了寫ng-template。
<ng-template [ngIf]="item.reminder"> <mat-icon > alarm </mat-icon> </ng-template> <!-- <mat-icon *ngIf="item.reminder"> alarm </mat-icon> -->
結(jié)構(gòu)型指令為什么能改變結(jié)構(gòu)?
ngIf源碼
set方法標記為@Input,如果條件為真而且不含view的話,把內(nèi)部hasView標識位置為true然后通過viewContainer根據(jù)template創(chuàng)建一個子view。
條件不為真就用視圖容器清空所含內(nèi)容。
viewContainerRef:容器,指令所在的視圖的容器
二,模塊Module
什么是模塊?獨立功能的文件集合,用來組織文件。
模塊元數(shù)據(jù)
entryComponents:進入模塊就要立刻加載的(比如對話框),而不是調(diào)用的時候加載。
exports:模塊內(nèi)部的想要讓大家公用,一定要export出來。
forRoot()是什么?
imports: [RouterModule.forRoot(routes)],
imports: [RouterModule.forChild(route)];
其實forRoot和forChild是兩個靜態(tài)工廠方法。
constructor(guard: any, router: Router); /** * Creates a module with all the router providers and directives. It also optionally sets up an * application listener to perform an initial navigation. * * Options (see `ExtraOptions`): * * `enableTracing` makes the router log all its internal events to the console. * * `useHash` enables the location strategy that uses the URL fragment instead of the history * API. * * `initialNavigation` disables the initial navigation. * * `errorHandler` provides a custom error handler. * * `preloadingStrategy` configures a preloading strategy (see `PreloadAllModules`). * * `onSameUrlNavigation` configures how the router handles navigation to the current URL. See * `ExtraOptions` for more details. * * `paramsInheritanceStrategy` defines how the router merges params, data and resolved data * from parent to child routes. */ static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>; /** * Creates a module with all the router directives and a provider registering routes. */ static forChild(routes: Routes): ModuleWithProviders<RouterModule>; }
元數(shù)據(jù)根據(jù)不同情況會變化,元數(shù)據(jù)沒辦法動態(tài)指定,不寫元數(shù)據(jù),直接構(gòu)造一個靜態(tài)的工程方法,返回一個Module。
寫一個forRoot()
創(chuàng)建一個serviceModule:$ ng g m services
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; @NgModule({ declarations: [], imports: [ CommonModule ] }) export class ServicesModule { }
ServiceModule里面的元數(shù)據(jù)不要了。用一個靜態(tài)方法forRoot返回。
import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; @NgModule() export class ServicesModule { static forRoot(): ModuleWithProviders{ return { ngModule: ServicesModule, providers:[] } } }
在core Module中導入的時候使用
imports: [ServicesModule.forRoot();]
三,風格定義
ngClass,ngStyle和[class.yourclass]
ngClass:用于條件動態(tài)指定樣式類,適合對樣式做大量更改的情況。預先定義好class。
<mat-list-item class="container" [@item]="widerPriority" [ngClass]="{ 'priority-normal':item.priority===3, 'priority-important':item.priority===2, 'priority-emergency':item.priority===1 }"
ngStyle:用于條件動態(tài)指定樣式,適合少量更改的情況。比如下面例子中[ngStyle]="{'order':list.order}"。key是一個字符串。
[class.yourclass] :[class.yourclass] = "condition"直接對應(yīng)一個條件。這個condition滿足適合應(yīng)用這個class。等價于ngClass的寫法,相當于是ngClass的變體,簡寫。
<div class="content" mat-line [class.completed]="item.completed"> <span [matTooltip]="item.desc">{{item.desc}}</span> </div>
使用ngStyle在拖拽的時候調(diào)整順序
原理就是動態(tài)指定flex容器樣式的order為list模型對象里的order。
1、在taskHome中給app-task-list添加order
list-container是一個flex容器,它的排列順序是按照order去排序的。
<app-task-list *ngFor="let list of lists" class="list-container" app-droppable="true" [dropTags]="['task-item','task-list']" [dragEnterClass]=" 'drag-enter' " [app-draggable]="true" [dragTag]=" 'task-list' " [draggedClass]=" 'drag-start' " [dragData]="list" (dropped)="handleMove($event,list)" [ngStyle]="{'order': list.order}" >
2、list數(shù)據(jù)結(jié)構(gòu)里需要有order,所以增加order屬性
lists = [ { id: 1, name: "待辦", order: 1, tasks: [ { id: 1, desc: "任務(wù)一: 去星巴克買咖啡", completed: true, priority: 3, owner: { id: 1, name: "張三", avatar: "avatars:svg-11" }, dueDate: new Date(), reminder: new Date() }, { id: 2, desc: "任務(wù)一: 完成老板布置的PPT作業(yè)", completed: false, priority: 2, owner: { id: 2, name: "李四", avatar: "avatars:svg-12" }, dueDate: new Date() } ] }, { id: 2, name: "進行中", order:2, tasks: [ { id: 1, desc: "任務(wù)三: 項目代碼評審", completed: false, priority: 1, owner: { id: 1, name: "王五", avatar: "avatars:svg-13" }, dueDate: new Date() }, { id: 2, desc: "任務(wù)一: 制定項目計劃", completed: false, priority: 2, owner: { id: 2, name: "李四", avatar: "avatars:svg-12" }, dueDate: new Date() } ] } ];
3、在list拖拽換順序的時候,改變order
交換兩個srcList和目標list的順序order
handleMove(srcData,targetList){ switch (srcData.tag) { case 'task-item': console.log('handling item'); break; case 'task-list': console.log('handling list'); const srcList = srcData.data; const tempOrder = srcList.order; srcList.order = targetList.order; targetList.order = tempOrder; default: break; } }
以上就是詳解Angular結(jié)構(gòu)型指令模塊和樣式的詳細內(nèi)容,更多關(guān)于Angular結(jié)構(gòu)型指令模塊和樣式的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用WebStorm進行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式)
這篇文章主要介紹了用WebStorm進行Angularjs 2開發(fā)(環(huán)境篇:Windows 10,Angular-cli方式),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12angularjs select 賦值 ng-options配置方法
下面小編就為大家分享一篇angularjs select 賦值 ng-options配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Angular.js實現(xiàn)注冊系統(tǒng)的實例詳解
Angular.js是Google開發(fā)的前端技術(shù)框架,最近一直在學習Angular.js,通過對angular.js的簡單理解后發(fā)現(xiàn),angular.js通過一些簡單的指令即可實現(xiàn)對DOM元素的操作,其特色為雙向數(shù)據(jù)綁定,下面這篇文章給大家詳細介紹Angular.js實現(xiàn)注冊系統(tǒng)的方法,一起來看看吧。2016-12-12Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程
在Angular的原生指令中有這幾個指令用來控制元素的展示與否,ng-show/ng-hide/ng-if和ng-switch。在angular性能優(yōu)化中,我們也常常會用到它。這篇文章主要給大家介紹了在Angular.JS中指令ng-if、ng-show/ng-hide和ng-switch的使用教程,需要的朋友可以參考。2017-05-05angular.js實現(xiàn)列表orderby排序的方法
今天小編就為大家分享一篇angular.js實現(xiàn)列表orderby排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10