Angular結構型指令,模塊和樣式

結構型指令

*是一個語法糖,<a *ngIf="user.login">退出</a>至關於數據結構

<ng-template [ngIf]="user.login">app

  <a>退出</a>ide

</ng-template>svg

避免了寫ng-template。flex

    <ng-template [ngIf]="item.reminder">
      <mat-icon >
        alarm
      </mat-icon>
    </ng-template>
    
    <!-- <mat-icon *ngIf="item.reminder">
      alarm
    </mat-icon> -->

結構型指令爲何能改變結構?spa

ngIf源碼code

set方法標記爲@Input,若是條件爲真並且不含view的話,把內部hasView標識位置爲true而後經過viewContainer根據template建立一個子view。orm

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

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

模塊

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

模塊元數據

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。

建立一個serviceModule:$ ng g m services

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule({
  declarations: [],
  imports: [
    CommonModule
  ]
})
export class ServicesModule { }

ServiceModule裏面的元數據不要了。用一個靜態方法forRoot返回。

import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';

@NgModule()
export class ServicesModule { 
  static forRoot(): ModuleWithProviders{
    return {
      ngModule: ServicesModule,
      providers:[]
    }
  }
}

導入的時候使用

imports: [ServicesModule.forRoot();]

風格定義

ngClass,ngStyle和[class.yourstyle]

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

<mat-list-item class="container" [@item]="widerPriority" [ngClass]="{
  'priority-normal':item.priority===3,
  'priority-important':item.priority===2,
  'priority-emergency':item.priority===1
}"
<div class="content" mat-line [ngClass]="{'completed':item.completed}">
    <span [matTooltip]="item.desc">{{item.desc}}</span>
</div>

ngStyle:用於條件動態指定樣式,適合少許更改的狀況。

[class.yourstyle] :

[class.yourcondition] = "condition"直接對應一個條件。這個condition知足適合應用這個class

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

使用ngStyle在拖拽的時候調整順序。

原理就是動態指定flex容器樣式的order爲list模型對象裏的order。

一、在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}"
  >

二、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()
        }
      ]
    }
  ];
View Code

 

三、在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;
    }
  }
相關文章
相關標籤/搜索