简体   繁体   English

无法绑定到“ngIf”,因为它不是“div”的已知属性 - 离子/角度错误

[英]Can't bind to 'ngIf' since it isn't a known property of 'div' - ionic/angular error

I've been getting this error for the last few days now.最近几天我一直收到这个错误。 I have read through other threads with the same error and tried what they suggested such as adding the CommonModule to both the current page and the app.module.我已经阅读了具有相同错误的其他线程并尝试了他们的建议,例如将 CommonModule 添加到当前页面和 app.module 中。 I have also tried resetting the server using ng serve.我也尝试过使用 ng serve 重置服务器。 Does anyone know why else i might have this issue?有谁知道为什么我可能会遇到这个问题? I have used more or less the same code in another part of the app and it works fine.我在应用程序的另一部分使用了或多或少相同的代码,它运行良好。

控制台错误截图

edit-page.module.ts:编辑page.module.ts:

    import { NgModule } from '@angular/core';
    import { CommonModule } from '@angular/common';
    import { FormsModule, ReactiveFormsModule } from '@angular/forms';
    
    import { IonicModule } from '@ionic/angular';
    
    import { EditPagePageRoutingModule } from './edit-page-routing.module';
import { BrowserModule } from '@angular/platform-browser';

    
    import { EditPagePage } from './edit-page.page';
    import { SharedModule } from 'src/app/shared/shared.module';
    import { AppComponent } from 'src/app/app.component';
    
    @NgModule({
      imports: [
        CommonModule,
        ReactiveFormsModule,
        FormsModule,
        IonicModule,
BrowserModule
        EditPagePageRoutingModule
      ],
      declarations: [EditPagePage]
    })
    export class EditPagePageModule {}

edit-page.page.ts:编辑page.page.ts:

@Component({
  selector: 'app-edit-page',
  templateUrl: './edit-page.page.html',
  styleUrls: ['./edit-page.page.scss'],
})
export class EditPagePage implements OnInit, OnDestroy {
  artist: Artist;
  artistId: string;
  arForm: FormGroup;
  isLoading = false;
  steps: any = 1;

  private artistSub: Subscription;

  constructor(
    private route: ActivatedRoute,
    private artistService: ArtistService,
    private navCtrl: NavController,
    private router: Router,
    private loadingCtrl: LoadingController,
    private alertCtrl: AlertController
  ) {}

  get genreControls() {
    return (<FormArray>this.arForm.get('genre')).controls;
  }

  get equipmentControls() {
    return (<FormArray>this.arForm.get('equipment')).controls;
  }

  ngOnInit() {
    this.route.paramMap.subscribe(paramMap => {
      if (!paramMap.has('artistId')) {
        this.navCtrl.navigateBack('/tabs/tab4');
        return;
      }
      this.artistId = paramMap.get('artistId');
      this.isLoading = true;
      this.artistSub = this.artistService
        .getArtist(paramMap.get('artistId'))
        .subscribe(
          artist => {
            this.artist = artist;
            this.arForm = new FormGroup({
              name: new FormControl(this.artist.name, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              type: new FormControl(this.artist.type, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              cost: new FormControl(this.artist.cost, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              equipment: new FormArray([]
                ),
              band: new FormControl(this.artist.band, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              availableFrom: new FormControl(this.artist.availableFrom, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              availableTo: new FormControl(this.artist.availableTo, {
                updateOn: 'blur',
                validators: [Validators.required]
              }),
              descrpition: new FormControl(this.artist.descrpition, {
                updateOn: 'blur',
                validators: [Validators.required, Validators.maxLength(180)]
              }),
              genre: new FormArray([]
                ),
            });
            this.isLoading = false;
          },
          error => {
            this.alertCtrl
              .create({
                header: 'An error occurred!',
                message: 'Artist could not be fetched. Please try again later.',
                buttons: [
                  {
                    text: 'Okay',
                    handler: () => {
                      this.router.navigate(['/tabs/tab4']);
                    }
                  }
                ]
              })
              .then(alertEl => {
                alertEl.present();
              });
          }
        );
    });
  }

  onAddGenre() {
    const control = new FormControl(this.artist.genre, [Validators.required]);
    (<FormArray>this.arForm.get('genre')).push(control);
  }

  onAddEquipment() {
    const control = new FormControl(this.artist.equipment, [Validators.required]);
    (<FormArray>this.arForm.get('equipment')).push(control);
  }

  addOne() {
    this.steps = this.steps + 1;
    console.log(this.steps);
  }

  minusOne() {
    this.steps = this.steps - 1;
    console.log(this.steps);
  }

  onUpdateArtist() {
    if (!this.arForm.valid) {
      return;
    }
    this.loadingCtrl
      .create({
        message: 'Updating artist...'
      })
      .then(loadingEl => {
        loadingEl.present();
        this.artistService
          .updateArtist(
            this.artist.id,
            this.arForm.value.name,
            this.arForm.value.type,
            this.arForm.value.cost,
            this.arForm.value.equipment,
            this.arForm.value.band,
            this.arForm.value.availableFrom,
            this.arForm.value.availableTo,
            this.arForm.value.descrpition,
            this.arForm.value.genre,
          )
          .subscribe(() => {
            loadingEl.dismiss();
            this.arForm.reset();
            this.router.navigate(['/tabs/tab4']);
          });
      });
  }

  ngOnDestroy() {
    if (this.artistSub) {
      this.artistSub.unsubscribe();
    }
  }
}

snippet of edit-page.page.html: edit-page.page.html 的片段:

<ion-content>
  <form [formGroup]="arForm">
    <ion-grid class="ion-padding">
      <div *ngIf="steps === 1">
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
            <h1>Thanks for joining, lets find you a venue</h1>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
            <ion-img
              class="pub-image"
              src="/assets/pub-front.png"
              alt="pub"
            ></ion-img>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
            <h1>Describe your ideal venue</h1>
            <p>Equipment, bar type, availibility etc.</p>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2" class="ion-text-center">
            <ion-button (click)="addOne()">Start</ion-button>
          </ion-col>
        </ion-row>
      </div>
      <!--
.
.
.
.
what type of bar page.
.
.
.
. -->
      <div *ngIf="steps === 2">
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2">
            <h1 class="ideal">YOUR IDEAL VENUE</h1>
            <h2>What is the name of your band?</h2>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col>
            <ion-item lines="none" type="text" class="box">
              <ion-input formControlName="name" required></ion-input>
            </ion-item>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col size="12" size-sm="8" offset-sm="2">
            <h2>What type of venue are you looking for?</h2>
          </ion-col>
        </ion-row>
        <ion-row>
          <ion-col>
            <ion-item>
              <ion-label>Select</ion-label>
              <ion-select
                formControlName="type"
                value="notifications"
                interface="action-sheet"
                required
              >
                <ion-select-option value="Pub">Pub</ion-select-option>
                <ion-select-option value="Late Bar">Late Bar</ion-select-option>
                <ion-select-option value="Club">Club</ion-select-option>
                <ion-select-option value="Wedding">Wedding</ion-select-option>
                <ion-select-option value="Event">Event</ion-select-option>
              </ion-select>
            </ion-item>
          </ion-col>
        </ion-row>
      </div>

App.module.ts: App.module.ts:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';
import { SharedModule } from './shared/shared.module';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [
    BrowserModule,
    CommonModule,
    FormsModule,
    HttpClientModule,
    IonicModule.forRoot(),
    AppRoutingModule,
    SharedModule,
    ReactiveFormsModule
  ],
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent],
})
export class AppModule { }

edited to add modules that didn't work编辑以添加不起作用的模块

Add the BrowserModule inside the imports in the edit-page.module.ts, otherwise, check if the component is registered in the app.module.ts在edit-page.module.ts的imports中添加BrowserModule,否则,检查组件是否在app.module.ts中注册

source 资源

暂无
暂无

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

相关问题 在控制台中出现错误无法绑定到“ngif”,因为它不是 angular 应用程序中“div”的已知属性 - Getting error in console Can't bind to 'ngif' since it isn't a known property of 'div' in angular app NX / Angular IntelliSense 的 VSCode IntelliSense 错误“无法绑定到 'ngIf',因为它不是 'div' 的已知属性” - VSCode IntelliSense error with NX / Angular IntelliSense "Can't bind to 'ngIf' since it isn't a known property of 'div'" (Ionic v4 + ionic-tooltips) - &gt;无法绑定到&#39;ngIf&#39;,因为它不是&#39;div&#39;的已知属性 - (Ionic v4 + ionic-tooltips) -> Can’t bind to ‘ngIf’ since it isn’t a known property of ‘div’ Angular 6 无法绑定到“*ngIf”,因为它不是已知属性 - Angular 6 Can't bind to '*ngIf' since it isn't a known property Angular 2无法绑定到'ngif',因为它不是已知属性 - Angular 2 Can't bind to 'ngif' since it isn't a known property NG0303:无法绑定到“ngIf”,因为它不是“div”的已知属性:Angular Modular Federation - NG0303: Can't bind to 'ngIf' since it isn't a known property of 'div' : Angular Modular Federation angular 2 控制台错误无法绑定到“ngIf”,因为它不是“span”的已知属性 - angular 2 console error Can't bind to 'ngIf' since it isn't a known property of 'span' Angular 抛出错误:无法绑定到“ngIf”,因为它不是“ng-container”的已知属性 - Angular throws error: can't bind to “ngIf” since it isn't a known property of “ng-container” 使用 Angular Monorepo 无法绑定到“ngIf”,因为它不是“div”的已知属性,并且“my-selector”不是已知元素 - With an Angular Monorepo Can't bind to 'ngIf' since it isn't a known property of 'div' and 'my-selector' is not a known element 无法绑定到“ngif”,因为它不是“span”的已知属性 - Can't bind to 'ngif' since it isn't a known property of 'span'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM