离子/角度:尽管语法正确

发布于 2025-01-30 09:05:17 字数 1580 浏览 4 评论 0原文

我正在开发一个具有角度的离子应用程序,并遇到以下错误:

my.page.ts(smippet)

export class MyPage implements OnInit {
  public myArray: Array<string> = [];

  constructor(){}

  ngOnInit(){
    this.updateArray();
  }

  updateArray() {
    // Performs API Call and puts the results into 'myArray'
  }
}

my.page.html(smippet)

<ion-select interface="popover">
  <ion-select-option *ngFor="let item of myArray">{{item}}</ion-select-option>
</ion-select>

我期望的是,阵列中包含的字符串被映射到“离子选择”的选择选项。但是,我的浏览器控制台显示了错误ng0303:无法绑定到“ ngforof”,因为它不是“离子 - 选项”的已知属性'

在研究该错误时,我只能找到NGFOR的较旧用法(等)是否不应再使用。但是,据我所知,我已经使用了最新的语法。

我正在使用npx离子服务运行我的应用 - LAB,使用Ionic与版本6和版本13 Angular一起运行。

我还尝试了不同的组件,例如'ion-text',但同样的错误也会发生。

另外,我验证了api-call的数据通过在HTTP-Request完成后打印在数组中正确存储。

我在做什么错?

提前致谢!

更新:

这是我的my.app.module-file:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

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

import { MyPageRoutingModule } from './my-routing.module';

import { MyPage } from './my.page';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    MyPageRoutingModule
  ],
  declarations: [MyPage]
})
export class MyPageModule {}

I am developing an Ionic App with Angular and ran into the following error:

my.page.ts (Snippet)

export class MyPage implements OnInit {
  public myArray: Array<string> = [];

  constructor(){}

  ngOnInit(){
    this.updateArray();
  }

  updateArray() {
    // Performs API Call and puts the results into 'myArray'
  }
}

my.page.html (Snippet)

<ion-select interface="popover">
  <ion-select-option *ngFor="let item of myArray">{{item}}</ion-select-option>
</ion-select>

What I expect is, that the strings contained in the array are mapped to selection options for the 'ion-select'. However, my browser console shows the error NG0303: Can't bind to 'ngForOf' since it isn't a known property of 'ion-select-option'

When researching that error, all I can find is the older usage of ngFor (item of myArray, etc.) that should no longer be used. However, I already use the up-to-date syntax as far as I understood.

I am running my app using npx ionic serve --lab, using Ionic with version 6 and Angular with version 13.

I also tried different components such as 'ion-text', but the same error occurs.

Also, I verified that the data from the API-Call is stored correctly in the array by printing it after the http-request has finished.

What am I doing wrong?

Thanks in advance!

UPDATE:

Here is my my.app.module-file:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

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

import { MyPageRoutingModule } from './my-routing.module';

import { MyPage } from './my.page';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    MyPageRoutingModule
  ],
  declarations: [MyPage]
})
export class MyPageModule {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2025-02-06 09:05:17

类型的错误无法绑定到“某物”,因为当您使用目标元素中不存在的属性或在此中不存在时,可能会发生“元素”的已知属性情况,因为您正在尝试使用找不到的属性指令。
在这种情况下,您正在尝试使用ngforof指令,但找不到它,因此您必须确保在组件的主机模块中导入该指令的主机模块。要求解此情况,您必须在模块中导入consionModule,您可以在其中声明mypage这样。

  import { CommonModule } from '@angular/common';

  @NgModule({
      declarations: [MyPage, ...],
      imports: [CommonModule, ...]
  })

编辑

对于为&lt; ion-select-option&gt;分配value很重要,因为默认值为undefined
我能够在a stackblitz 并在未分配value时遇到相同的错误。

This error of type Can't bind to 'something' since it isn't a known property of 'element' may occur when you are using an attribute that not exist in the target element or like in this case, because you are trying to use an attribute directive that can't be found.
In this case you are trying to use ngForOf directive but it can't be found, so you have to ensure that you are importing the host module for that directive in the host module of your component. To solve this case you have to import CommonModule in the module where you declare MyPage like this.

  import { CommonModule } from '@angular/common';

  @NgModule({
      declarations: [MyPage, ...],
      imports: [CommonModule, ...]
  })

EDIT

Is important to assign a value for <ion-select-option> because the default is undefined.
I was able to reproduce your problem on a stackblitz and got the same error when not assign a value.

再可℃爱ぅ一点好了 2025-02-06 09:05:17

您需要致电NGIF,以确保在MyArray数据准备就绪之前没有加载数据。

将您的HTML代码更改为下面,以检查数组中是否存在任何数据,如果没有,则在数据之前将保持隐藏和不活动。

<ion-select *ngIf="myArray.length > 0" interface="popover">
     <ion-select-option *ngFor="let item of myArray">{{item}}</ion-select-option>
</ion-select>

You need to call ngIf to make sure the data isn't being loaded BEFORE your myArray data is ready.

Change your HTML code to below to check if there is any data exists in your array, if not, it will remain hidden and inactive until there is a data.

<ion-select *ngIf="myArray.length > 0" interface="popover">
     <ion-select-option *ngFor="let item of myArray">{{item}}</ion-select-option>
</ion-select>
美男兮 2025-02-06 09:05:17

我只是想简要更新!

最终,这个问题与角度/离子有关。取而代之的是,问题是我写的要更新数组的功能是传统的,而不是箭头功能。

因此,“这个”属于不同的上下文,因此,通过覆盖UI绑定的数组,实际上创建了一个新的变量。

因此,UI不受更改的影响。将功能更改为箭头功能解决了问题。

I just wanted to give a short update!

Eventually, the issue was not really Angular/Ionic-related. Instead, the issue was that the function I wrote to update the array was a traditional one instead of an arrow function.

Due to that, 'this' belonged to a different context, so by overriding the array on which the UI is bound, actually a new variable was created.

The UI therefore wasn't affected by the change. Changing the function to an arrow function solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文