简体   繁体   English

在 Angular 中使用 cdkDropListData 时出错,无法绑定到“cdkDropListData”,因为它不是“div”的已知属性

[英]Error when using cdkDropListData in Angular gives me Can't bind to 'cdkDropListData' since it isn't a known property of 'div'

<div
                      cdkDropList
                      #girlList="cdkDropList"
                      [cdkDropListData]="girls"
                      [cdkDropListConnectedTo]="[convaList]"
                      class="example-list"
                      (cdkDropListDropped)="drop($event)"><div class="card color-challenging mb-2" *ngFor="let girls_data of girls" cdkDrag>
                      <div class="card-body p-2 justify-content-between align-items-center d-flex">
                        <span class="reading-grade font-weight-bold">{{girls_data.id}}</span>
                        <div class="student-grade flex flex-grow-1">
                          <p class="justify-content-between align-items-center d-flex">
                            <span class="student-name">{{girls_data.firstName}}{{girls_data.lastName}}</span>
                            <span>{{girls_data.gender}}</span>
                          </p>
                          <p class="justify-content-between align-items-center d-flex">
                            <span>{{girls_data.currentAcademicYear}}</span>
                            <span><i class="fa fa-ban" aria-hidden="true"></i> <i class="fa fa-paperclip" aria-hidden="true"></i></span>
                          </p>
                        </div>
                        <span class="behavior-grade text-right font-weight-bold">{{girls_data.inGrade}}</span>
                      </div>
                    </div>

When using [cdkDropListData] here gives me error on console that Can't bind to 'cdkDropListData' since it isn't a known property of 'div'.当在这里使用 [cdkDropListData] 时,控制台上出现错误,无法绑定到“cdkDropListData”,因为它不是“div”的已知属性。

I am new to angular so please avoid newbie behaviour我是 angular 的新手,所以请避免新手行为

I already imported the CdkDragDrop in module.ts我已经在 module.ts 中导入了 CdkDragDrop

This is the Component file.这是组件文件。

import {Component, NgModule} from '@angular/core';
import {StudentModel} from '../model/studentRepository.model';
import {Student} from '../model/student.model';
import {CdkDragDrop, DragDropModule, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';


@Component({
  selector: 'student-selector',
  templateUrl: 'studentSelector.component.html',
  styleUrls: ['./studentSelector.component.css']
})

export class StudentSelector {
  boys =  [];
  girls = [];
  constructor(private dataModel: StudentModel) {
    this.boys = dataModel.getStudents();
    this.girls = dataModel.getStudents();

  }
  get students(): Student[] {
    return this.dataModel.getStudents();
  }
  conva = [];
  
  drop(event : CdkDragDrop<string[]>){
    transferArrayItem(event.previousContainer.data,
      event.container.data,
      event.previousIndex,
      event.currentIndex);
  }
}

Here is the module file.这是模块文件。

import {NgModule} from '@angular/core';
import { StudentModel } from './studentRepository.model';
import { SimpleDataSource } from './datasource.model';
import {DragDropModule} from '@angular/cdk/drag-drop';


@NgModule({
  providers: [StudentModel,SimpleDataSource],
  imports : [DragDropModule]
})

export class ModelModule {
  
}

The imports should be like this:导入应该是这样的:

import {NgModule} from '@angular/core';
import { StudentModel } from './studentRepository.model';
import { SimpleDataSource } from './datasource.model';
import {DragDropModule} from '@angular/cdk/drag-drop';


@NgModule({
  providers: [StudentModel,SimpleDataSource],
  imports : [DragDropModule]
})

export class ModelModule {
  
}

and (see notes in the code)和(见代码中的注释)

import {Component} from '@angular/core';    <= NgModule removed
import {StudentModel} from '../model/studentRepository.model';
import {Student} from '../model/student.model';
import {CdkDragDrop, moveItemInArray, transferArrayItem} from '@angular/cdk/drag-drop';     <= DragDropModule removed


@Component({
  selector: 'student-selector',
  templateUrl: 'studentSelector.component.html',
  styleUrls: ['./studentSelector.component.css']
})

export class StudentSelector {
  ...
}

Generally:一般来说:
xxxModule should only ever be imported on module level, not on component level. xxxModule 应该只在模块级别导入,而不是在组件级别。

Also, as I mentioned in the comments:另外,正如我在评论中提到的:
It looks like your StudentSelector is in a different module than your ModelModule (at least it is not part of the declarations you provided).看起来您的ModelModule与您的StudentSelector位于不同的模块中(至少它不是您提供的声明的一部分)。 A component can only be used in the module that declares it (declarations-list) OR that imports another module which in return declares the component and exports it (exports-list).组件只能在声明它的模块中使用(声明列表)或导入另一个模块,该模块反过来声明该组件并导出它(导出列表)。

import this in your app module在您的应用程序模块中导入它

import { DragDropModule} from '@angular/cdk/drag-drop';

and re run the app.并重新运行应用程序。

This is not very technical solution but there are few things which I've faced.这不是非常技术性的解决方案,但我面临的事情很少。

1. Clear the cache before load the page (If you are not clearing it usually). 1.在加载页面之前清除缓存(如果您通常不清除它)。 Sometimes it misses to fetch the data to the variable.有时它会错过将数据提取到变量中。

2. Fix the run time bugs in the current page (may be in the another block but in the same page). 2. 修复当前页面的运行时bug(可能在另一个block但在同一个页面)。

But I've faced the first issue.但是我遇到了第一个问题。 when I clear the cache it worked.当我清除缓存时它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2 RC 5 - 无法绑定到&#39;...&#39;,因为它不是&#39;...&#39;的已知属性 - Angular 2 RC 5 - Can't bind to '…' since it isn't a known property of '…' 使用ng2-bootstrap与Angular 2 RC 6 - 无法绑定到无法绑定到[...],因为它不是[...]的已知属性 - Using ng2-bootstrap with Angular 2 RC 6 - can't bind to can't bind to […] since it isn't a known property of […] 无法绑定到 &#39;dtOptions&#39;,因为它不是 &#39;table&#39; 的已知属性。 - Can't bind to 'dtOptions' since it isn't a known property of 'table'. Angular 2 - 无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Angular 2 - Can't bind to 'ngModel' since it isn't a known property of 'input' 无法绑定到“ ngModule”,因为它不是“ input”的已知属性。 在angular 2 asp.net核心中 - Can't bind to 'ngModule' since it isn't a known property of 'input'. in angular 2 asp.net core 2.0.0版本无法绑定到&#39;ngIf&#39;,因为它不是&#39;div&#39;的已知属性 - 2.0.0 version Can't bind to 'ngIf' since it isn't a known property of 'div' Angular2 RC5教程:无法绑定到'ngModel',因为它不是'input'的已知属性 - Angular2 RC5 tutorial : Can't bind to 'ngModel' since it isn't a known property of 'input' 无法绑定到“ngForOf”,因为它不是 Ionic / Angular 9 中“ion-item”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'ion-item' in Ionic / Angular 9 离子图标和“无法绑定到&#39;名称&#39;,因为它不是已知的本机属性”错误 - ionic icon and “Can't bind to 'name' since it isn't a known native property” error 错误:模板解析错误:无法绑定到“选项”,因为它不是“图表”的已知属性 - Error: Template parse errors: Can't bind to 'options' since it isn't a known property of 'chart'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM