Angular7實(shí)現(xiàn)拖放Drag?Drop示例詳解
拖放函數(shù)
Angular 7 CDK中新增的拖放函數(shù)有助于從列表中拖放元素,我們將通過一個(gè)示例來理解拖放函數(shù)。我們需要先下載依賴項(xiàng),如下所示-
npm install @angular/cdk --save

拖放模塊導(dǎo)入app.module.ts
完成上述步驟后。讓我們將拖放模塊導(dǎo)入app.module.ts中
如下所示:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule , RoutingComponent} from './app-routing.module';
import { AppComponent } from './app.component';
import { NewCmpComponent } from './new-cmp/new-cmp.component';
import { ChangeTextDirective } from './change-text.directive';
import { SqrtPipe } from './app.sqrt';
import { MyserviceService } from './myservice.service';
import { HttpClientModule } from '@angular/common/http';
import { ScrollDispatchModule } from '@angular/cdk/scrolling';
import { DragDropModule } from '@angular/cdk/drag-drop';
@NgModule({
declarations: [
SqrtPipe,
AppComponent,
NewCmpComponent,
ChangeTextDirective,
RoutingComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
ScrollDispatchModule,
DragDropModule
],
providers: [MyserviceService],
bootstrap: [AppComponent]
})
export class AppModule { }DragDropModule是從'@angular/cdk/drag-drop'導(dǎo)入的,并且該模塊已添加到導(dǎo)入數(shù)組,如上所示。
myservice.service.ts
<a target="_blank" rel="external nofollow" rel="external nofollow" rel="external nofollow" >import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class MyserviceService {
private finaldata=[];
private apiurl="http://jsonplaceholder.typicode.com/users";
constructor(private http: HttpClient) { }
getData() {
return this.http.get(this.apiurl);
}
}</a>調(diào)用app.component.ts內(nèi)部的服務(wù)
完成后,調(diào)用app.component.ts內(nèi)部的服務(wù),如下所示-
<a target="_blank" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
import { Component } from '@angular/core';
import { MyserviceService } from './myservice.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title='Angular 7 Project!';
public personaldetails=[];
constructor(private myservice: MyserviceService) {}
ngOnInit() {
this.myservice.getData().subscribe((data) => {
this.personaldetails=Array.from(Object.keys(data), k=>data[k]);
console.log(this.personaldetails);
});
}
}
</a>我們?cè)趐ersonaldetails變量中提供了所需的數(shù)據(jù)?,F(xiàn)在讓我們使用它來顯示給用戶,如下所示-
<a target="_blank" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<h3>Angular 7 - Drag and Drop Module</h3>
<div>
<div *ngFor="let item of personaldetails; let i=index" class="divlayout” >
{{item.name}}
</div>
</div>
</a>我們添加了class =" divlayout",該類的詳細(xì)信息位于app.component.css中。
.divlayout{
width: 40%;
background-color: #ccc;
margin-bottom: 5px;
padding: 10px 10px;
border: 3px solid #73AD21;
}以下屏幕將顯示在瀏覽器中-

在app.component.html中添加dragdrop cdk屬性
它不會(huì)拖放任何東西,我們需要在app.component.html中添加dragdrop cdk屬性,如下所示-
<h3>Angular 7 - Drag and Drop Module</h3>
<div cdkDropList
#personList="cdkDropList"
[cdkDropListData]="personaldetails"
[cdkDropListConnectedTo]="[userlist]"
class="example-list"
(cdkDropListDropped)="onDrop($event)" >
<div *ngFor="let item of personaldetails;
let i=index" class="divlayout" cdkDrag>
{{item.name}}
</div>
</div>突出顯示的是執(zhí)行拖放所需的所有屬性。當(dāng)您在瀏覽器中簽入時(shí),它允許您拖動(dòng)項(xiàng)目。它不會(huì)將其放置在列表中,并且保留在您離開鼠標(biāo)指針時(shí)的狀態(tài)。

在這里,它允許從列表中拖動(dòng)項(xiàng)目,但是一旦您離開鼠標(biāo)指針,它將移至同一位置。要添加放置函數(shù),我們需要在app.component.ts中添加onDrop事件,如下所示-
首先,我們必須導(dǎo)入dragdrap cdk模塊,如下所示-
import {CdkDragDrop, moveItemInArray, transferArrayItem}
from '@angular/cdk/drag-drop';這是app.component.ts中的完整代碼
import { Component } from '@angular/core';
import { MyserviceService } from './myservice.service';
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title='Angular 7 Project!';
public personaldetails=[];
constructor(private myservice: MyserviceService) {}
ngOnInit() {
this.myservice.getData().subscribe((data) => {
this.personaldetails=Array.from(Object.keys(data),
k=>data[k]);
console.log(this.personaldetails);
});
}
onDrop(event: CdkDragDrop<string[]>) {
if (event.previousContainer === event.container) {
moveItemInArray(event.container.data,
event.previousIndex, event.currentIndex);
} else {
transferArrayItem(event.previousContainer.data,
event.container.data,
event.previousIndex,
event.currentIndex);
}
}
}onDrop函數(shù)負(fù)責(zé)將拖動(dòng)的項(xiàng)目放置在所需位置。
它利用了我們從cdk dragdrop模塊導(dǎo)入的 moveItemInArray 和 transferArrayItem 。
現(xiàn)在讓我們?cè)跒g覽器中再次查看演示-

現(xiàn)在,您可以將項(xiàng)目拖放到所需的位置,如上所示。該函數(shù)運(yùn)行非常平穩(wěn),沒有任何閃爍問題,可以在需要時(shí)在您的應(yīng)用程序中使用。
以上就是Angular7實(shí)現(xiàn)拖放Drag Drop示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Angular 拖放Drag Drop的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
angularJS?實(shí)現(xiàn)長按不觸發(fā)點(diǎn)擊事件可以復(fù)制剪貼方法
這篇文章主要為大家介紹了angularJS實(shí)現(xiàn)長按不觸發(fā)點(diǎn)擊事件可以復(fù)制剪貼方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程
本篇文章主要介紹了詳解基于Angular4+ server render(服務(wù)端渲染)開發(fā)教程 ,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Angularjs 滾動(dòng)加載更多數(shù)據(jù)
AngularJS 通過新的屬性和表達(dá)式擴(kuò)展了 HTML。本文主要給大家介紹Angularjs 滾動(dòng)加載更多數(shù)據(jù)的相關(guān)知識(shí),需要的朋友參考下吧2016-03-03
詳解Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問題
這篇文章主要為大家介紹了Angular組件數(shù)據(jù)不能實(shí)時(shí)更新到視圖上的問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
AngularJS實(shí)現(xiàn)表單元素值綁定操作示例
這篇文章主要介紹了AngularJS實(shí)現(xiàn)表單元素值綁定操作,結(jié)合具體實(shí)例形式分析了AngularJS針對(duì)表單元素屬性相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Angular6 用戶自定義標(biāo)簽開發(fā)的實(shí)現(xiàn)方法
這篇文章主要介紹了Angular6 用戶自定義標(biāo)簽開發(fā)的實(shí)現(xiàn)方法,下面我們就通過一個(gè)簡單的例子演示Angular6中的這一新功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,需要的朋友可以參考下2019-01-01

