摘要:實(shí)現(xiàn)組件位置交換中視圖是和數(shù)據(jù)綁定的,它并不推薦我們直接操作元素,而且推薦我們通過(guò)操作數(shù)據(jù)的方式來(lái)改變組件視圖。首先定義兩個(gè)組件按鈕我們?cè)谙旅娴拇a中,動(dòng)態(tài)創(chuàng)建以上兩個(gè)組件,并實(shí)現(xiàn)位置交換功能。
這篇文章主要介紹了angular6 利用 ngContentOutlet 實(shí)現(xiàn)組件位置交換(重排),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
ngContentOutlet指令介紹
ngContentOutlet指令與ngTemplateOutlet指令類(lèi)似,都用于動(dòng)態(tài)組件,不同的是,前者傳入的是一個(gè)Component,后者傳入的是一個(gè)TemplateRef。
首先看一下使用:
其中MyComponent是我們自定義的組件,該指令會(huì)自動(dòng)創(chuàng)建組件工廠,并在ng-container中創(chuàng)建視圖。
實(shí)現(xiàn)組件位置交換
angular中視圖是和數(shù)據(jù)綁定的,它并不推薦我們直接操作HTML DOM元素,而且推薦我們通過(guò)操作數(shù)據(jù)的方式來(lái)改變組件視圖。
首先定義兩個(gè)組件:
button.component.ts
import { Component, OnInit } from "@angular/core"; @Component({ selector: "app-button", template: ``, styleUrls: ["./button.component.css"] }) export class ButtonComponent implements OnInit { constructor() { } ngOnInit() {
text.component.ts
import { Component, OnInit, Input } from "@angular/core"; @Component({ selector: "app-text", template: ` `, styleUrls: ["./text.component.css"] }) export class TextComponent implements OnInit { @Input() public textName = "null"; constructor() { } ngOnInit() { } }
我們?cè)谙旅娴拇a中,動(dòng)態(tài)創(chuàng)建以上兩個(gè)組件,并實(shí)現(xiàn)位置交換功能。
動(dòng)態(tài)創(chuàng)建組件,并實(shí)現(xiàn)位置交換
我們先創(chuàng)建一個(gè)數(shù)組,用于存放上文創(chuàng)建的兩個(gè)組件ButtonComponent和TextComponent,位置交換時(shí),只需要調(diào)換組件在數(shù)組中的位置即可,代碼如下:
import { TextComponent } from "./text/text.component"; import { ButtonComponent } from "./button/button.component"; import { Component } from "@angular/core"; @Component({ selector: "app-root", template: `
`, styleUrls: ["./app.component.css"] }) export class AppComponent { public componentArr = [TextComponent, ButtonComponent]; constructor() { } public swap() { const temp = this.componentArr[0]; this.componentArr[0] = this.componentArr[1]; this.componentArr[1] = temp; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/40196.html
摘要:實(shí)現(xiàn)組件位置交換中視圖是和數(shù)據(jù)綁定的,它并不推薦我們直接操作元素,而且推薦我們通過(guò)操作數(shù)據(jù)的方式來(lái)改變組件視圖。首先定義兩個(gè)組件按鈕我們?cè)谙旅娴拇a中,動(dòng)態(tài)創(chuàng)建以上兩個(gè)組件,并實(shí)現(xiàn)位置交換功能。 這篇文章主要介紹了angular6 利用 ngContentOutlet 實(shí)現(xiàn)組件位置交換(重排),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧 ngConten...
摘要:實(shí)現(xiàn)組件位置交換中視圖是和數(shù)據(jù)綁定的,它并不推薦我們直接操作元素,而且推薦我們通過(guò)操作數(shù)據(jù)的方式來(lái)改變組件視圖。首先定義兩個(gè)組件按鈕我們?cè)谙旅娴拇a中,動(dòng)態(tài)創(chuàng)建以上兩個(gè)組件,并實(shí)現(xiàn)位置交換功能。 ngContentOutlet指令介紹 ngContentOutlet指令與ngTemplateOutlet指令類(lèi)似,都用于動(dòng)態(tài)組件,不同的是,前者傳入的是一個(gè)Component,后者傳入的...
摘要:在使用進(jìn)行開(kāi)發(fā)的時(shí)候通過(guò)屬性綁定向組件內(nèi)部傳值的方式有時(shí)候并不能完全滿(mǎn)足需求比如我們寫(xiě)了一個(gè)公共組件但是某個(gè)模板使用這個(gè)公共組件的時(shí)候需要在其內(nèi)部添加一些標(biāo)簽內(nèi)容這種情況下除了使用預(yù)先在組件內(nèi)部定義之外就可以利用指令向組件傳入內(nèi)容指令類(lèi)似于 在使用angular進(jìn)行開(kāi)發(fā)的時(shí)候,通過(guò)屬性綁定向組件內(nèi)部傳值的方式,有時(shí)候并不能完全滿(mǎn)足需求,比如我們寫(xiě)了一個(gè)公共組件,但是某個(gè)模板使用這個(gè)公共...
閱讀 3295·2023-04-26 01:30
閱讀 771·2021-11-08 13:15
閱讀 1904·2021-09-24 10:35
閱讀 1072·2021-09-22 15:41
閱讀 1983·2019-08-30 15:44
閱讀 693·2019-08-30 13:22
閱讀 1069·2019-08-30 13:06
閱讀 1280·2019-08-29 13:22