欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Angular結(jié)構(gòu)型指令模塊和樣式

 更新時(shí)間:2021年05月21日 15:15:49   作者:starof  
本文主要介紹了Angular的結(jié)構(gòu)性指令模塊和樣式,對(duì)此感興趣的同學(xué),可以參考下。

一,結(jié)構(gòu)型指令

*是一個(gè)語法糖,<a *ngIf="user.login">退出</a>相當(dāng)于

<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方法標(biāo)記為@Input,如果條件為真而且不含view的話,把內(nèi)部hasView標(biāo)識(shí)位置為true然后通過viewContainer根據(jù)template創(chuàng)建一個(gè)子view。

條件不為真就用視圖容器清空所含內(nèi)容。

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

二,模塊Module

什么是模塊?獨(dú)立功能的文件集合,用來組織文件。

模塊元數(shù)據(jù)

entryComponents:進(jìn)入模塊就要立刻加載的(比如對(duì)話框),而不是調(diào)用的時(shí)候加載。

exports:模塊內(nèi)部的想要讓大家公用,一定要export出來。

forRoot()是什么?

imports: [RouterModule.forRoot(routes)],

imports: [RouterModule.forChild(route)];

其實(shí)forRoot和forChild是兩個(gè)靜態(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ù)不同情況會(huì)變化,元數(shù)據(jù)沒辦法動(dòng)態(tài)指定,不寫元數(shù)據(jù),直接構(gòu)造一個(gè)靜態(tài)的工程方法,返回一個(gè)Module。

寫一個(gè)forRoot()

創(chuàng)建一個(gè)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ù)不要了。用一個(gè)靜態(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中導(dǎo)入的時(shí)候使用

imports: [ServicesModule.forRoot();]

三,風(fēng)格定義

ngClass,ngStyle和[class.yourclass]

ngClass:用于條件動(dòng)態(tài)指定樣式類,適合對(duì)樣式做大量更改的情況。預(yù)先定義好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:用于條件動(dòng)態(tài)指定樣式,適合少量更改的情況。比如下面例子中[ngStyle]="{'order':list.order}"。key是一個(gè)字符串。

[class.yourclass] :[class.yourclass] = "condition"直接對(duì)應(yīng)一個(gè)條件。這個(gè)condition滿足適合應(yīng)用這個(gè)class。等價(jià)于ngClass的寫法,相當(dāng)于是ngClass的變體,簡(jiǎn)寫。

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

使用ngStyle在拖拽的時(shí)候調(diào)整順序

原理就是動(dòng)態(tài)指定flex容器樣式的order為list模型對(duì)象里的order。

1、在taskHome中給app-task-list添加order

list-container是一個(gè)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: "進(jìn)行中",
      order:2,
      tasks: [
        {
          id: 1,
          desc: "任務(wù)三: 項(xiàng)目代碼評(píng)審",
          completed: false,
          priority: 1,
          owner: {
            id: 1,
            name: "王五",
            avatar: "avatars:svg-13"
          },
          dueDate: new Date()
        },
        {
          id: 2,
          desc: "任務(wù)一: 制定項(xiàng)目計(jì)劃",
          completed: false,
          priority: 2,
          owner: {
            id: 2,
            name: "李四",
            avatar: "avatars:svg-12"
          },
          dueDate: new Date()
        }
      ]
    }
  ];

3、在list拖拽換順序的時(shí)候,改變order

交換兩個(gè)srcList和目標(biāo)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)型指令模塊和樣式的詳細(xì)內(nèi)容,更多關(guān)于Angular結(jié)構(gòu)型指令模塊和樣式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論