首頁 > web前端 > js教程 > 主體

詳解Angular中的結構型指令、模組和樣式

青灯夜游
發布: 2021-02-22 17:55:53
轉載
2239 人瀏覽過

詳解Angular中的結構型指令、模組和樣式

相關推薦:《angular教學

#一,結構型指令

*是一個語法糖,退出相當於

<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> -->
登入後複製

結構型指令為什麼能改變結構?

ngIf原始碼

set方法標記為@Input,如果條件為真且不含view的話,把內部hasView標識位置為true然後透過viewContainer根據template建立一個子view。

條件不為真就用視圖容器清空所含內容。

viewContainerRef:容器,指令所在的視圖的容器

二,模組Module

什麼是模組?獨立功能的文件集合,用來組織文件。

模組元資料

entryComponents:進入模組就要立刻載入的(例如對話框),而不是呼叫的時候載入。

exports:模組內部的想要讓大家公用,一定要export出來。

forRoot()是什麼?

imports: [RouterModule.forRoot(routes)],

imports: [RouterModule.forChild(route)];

其實forRoot和forChild是兩個靜態工廠方法。

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>;
}
登入後複製

元資料根據不同情況會變化,元資料沒辦法動態指定,不寫元數據,直接建構一個靜態的工程方法,回傳一個Module。

寫一個forRoot()

建立一個serviceModule:$ ng g m services

import { NgModule } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class ServicesModule { }
登入後複製

ServiceModule裡面的元資料不要了。用一個靜態方法forRoot回傳。

import { NgModule, ModuleWithProviders } from &#39;@angular/core&#39;;
import { CommonModule } from &#39;@angular/common&#39;;

@NgModule()
export class ServicesModule { 
  static forRoot(): ModuleWithProviders{
    return {
      ngModule: ServicesModule,
      providers:[]
    }
  }
}
登入後複製

在core Module中導入的時候使用

imports: [ServicesModule.forRoot();]

三,風格定義

ngClass,ngStyle和[class.yourclass]

ngClass:用於條件動態指定樣式類,適合對樣式做大量變更的情況。預先定義好class。

<mat-list-item class="container" [@item]="widerPriority" [ngClass]="{
  &#39;priority-normal&#39;:item.priority===3,
  &#39;priority-important&#39;:item.priority===2,
  &#39;priority-emergency&#39;:item.priority===1
}"
登入後複製
<div class="content" mat-line [ngClass]="{&#39;completed&#39;:item.completed}">
    <span [matTooltip]="item.desc">{{item.desc}}</span>
</div>
登入後複製

ngStyle:用於條件動態指定樣式,適合少量變更的情況。例如下面範例中[ngStyle]="{'order':list.order}"。 key是一個字串。

[class.yourclass] :[class.yourclass] = "condition"直接對應一個條件。這個condition滿足適合應用這個class。等價於ngClass的寫法,相當於是ngClass的變體,簡寫。

<div class="content" mat-line [class.completed]="item.completed">
    <span [matTooltip]="item.desc">{{item.desc}}</span>
</div>
登入後複製

1,使用ngStyle在拖曳的時候調整順序。

原理就是動態指定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]="[&#39;task-item&#39;,&#39;task-list&#39;]"
  [dragEnterClass]=" &#39;drag-enter&#39; "
  [app-draggable]="true"
  [dragTag]=" &#39;task-list&#39; "
  [draggedClass]=" &#39;drag-start&#39; "
  [dragData]="list"
  (dropped)="handleMove($event,list)"
  [ngStyle]="{&#39;order&#39;: list.order}"
  >
登入後複製

2、list資料結構裡需要有order,所以增加order屬性

lists = [
    {
      id: 1,
      name: "待办",
      order: 1,
      tasks: [
        {
          id: 1,
          desc: "任务一: 去星巴克买咖啡",
          completed: true,
          priority: 3,
          owner: {
            id: 1,
            name: "张三",
            avatar: "avatars:svg-11"
          },
          dueDate: new Date(),
          reminder: new Date()
        },
        {
          id: 2,
          desc: "任务一: 完成老板布置的PPT作业",
          completed: false,
          priority: 2,
          owner: {
            id: 2,
            name: "李四",
            avatar: "avatars:svg-12"
          },
          dueDate: new Date()
        }
      ]
    },
    {
      id: 2,
      name: "进行中",
      order:2,
      tasks: [
        {
          id: 1,
          desc: "任务三: 项目代码评审",
          completed: false,
          priority: 1,
          owner: {
            id: 1,
            name: "王五",
            avatar: "avatars:svg-13"
          },
          dueDate: new Date()
        },
        {
          id: 2,
          desc: "任务一: 制定项目计划",
          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 &#39;task-item&#39;:
        console.log(&#39;handling item&#39;);
        break;
      case &#39;task-list&#39;:
        console.log(&#39;handling list&#39;);
        const srcList = srcData.data;
        const tempOrder = srcList.order;
        srcList.order = targetList.order;
        targetList.order = tempOrder;
      default:
        break;
    }
  }
登入後複製

更多程式相關知識,請造訪:程式設計影片! !

以上是詳解Angular中的結構型指令、模組和樣式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:cnblogs.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!