我想要在Angular 中使用for循环,这是我在app.component.ts 中的代码

enter image description here

export class AppComponent {
  title = 'CatsBookProject';


    postText =[
      'Hello , i am here to meet new friends and make a friends relationship',
      'Hello everyone, Iam glad you are here',
      'Hey, my name is Susanne. I amm 2 years old',
         'I like eating cookies.'
     ]
    postImgs = [
      'assets/Imge/img1.png',
      'assets/Imge/im21.png',
      'assets/Imge/img3.jpg',
      'assets/Imge/img4.jpg',
     ]


   buttonClicked(){
    alert("What's up");
  }
}
and this in `app.componwnt.html`
[![enter image description here](https://i.stack.imgur.com/0IDE4.png)](https://i.stack.imgur.com/0IDE4.png)

<app-stroy*ngFor=‘let i of[0,1,2,3]’[img]=‘postImgs[0]’[Text]=‘postText[0]’>


and I have this error 

[![enter image description here](https://i.stack.imgur.com/a0PPp.png)](https://i.stack.imgur.com/a0PPp.png)


NG0303:无法绑定到‘ngForOf’,因为它不是‘app-stroy’的已知属性(在‘_AppComponent’组件模板中使用).

  1. 如果‘app-stroy’是一个Angular 组件,并且它具有‘ngForOf’输入,则验证它是否包含在该组件的‘@Component.Imports’中.
  2. 如果‘app-stroy’是Web组件,则将‘CUSTOM_ELEMENTS_SCHEMA’添加到此组件的‘@Component.schemas’以取消显示此消息.
  3. 若要允许任何属性,请将‘NO_ERROR_SCHEMA’添加到此组件的‘@Component.schemas’中.
this is the code in `story.component.ts `

    import { Component, Input, OnInit } from '@angular/core';
    @Component({
    selector: 'app-stroy',
    standalone: true,
    imports: [],
    templateUrl: './stroy.component.html',
    styleUrl: './stroy.component.scss'
    })
    export class StroyComponent implements OnInit{
    @Input() text:string='';
    @Input() img :string='';
    constructor(){ }
    ngOnInit(): void {
    }}

and this is in 

> app.component.ts

    import { Component } from '@angular/core';
    import { RouterOutlet } from '@angular/router';
    import {HeaderComponent} from './header/header.component';
    import {StroyComponent} from './stroy/stroy.component';
    import { NgModel } from '@angular/forms';

    @Component({
    selector: 'app-root',
    standalone: true,
    imports: [RouterOutlet, HeaderComponent, StroyComponent],
    templateUrl: './app.component.html',
    styleUrl: './app.component.scss'
    })
    export class AppComponent {
    title = 'CatsBookProject';
    postText =[
      'Hello , i am here to meet new friends and make a friends         relationship',
      'Hello everyone, Iam glad you are here',
      'Hey, my name is Susanne. I amm 2 years old',
         'I like eating cookies.'
     ]
    postImgs = [
      'assets/Imge/img1.png',
      'assets/Imge/im21.png',
      'assets/Imge/img3.jpg',
      'assets/Imge/img4.jpg',
     ]
   buttonClicked(){
    alert("What's up");
   }}

maybe some one know why ??
I need some help.

推荐答案

您应该在AppComponentimports数组中包括NgForOfCommonModule.

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

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet, HeaderComponent, StroyComponent, NgForOf, CommonModule],
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss',
})
export class AppComponent {

}

Demo @ StackBlitz

Typescript相关问答推荐

TS 2339:属性切片不存在于类型DeliverableSignal产品[]上>'

有没有办法适当缩小类型?

typescribe警告,explate—deps,useEffect依赖项列表

使用`renderToPipeableStream`时如何正确恢复服务器端Apollo缓存?

Angular 错误NG0303在Angular 项目中使用app.Component.html中的NGFor

如何在typescript中设置对象分配时的键类型和值类型

如何消除在Next.js中使用Reaction上下文的延迟?

类型脚本映射类型:从对象和字符串列表到键值对象

表单嵌套太深

有没有可能产生输出T,它在视觉上省略了T中的一些键?

如何在已知基类的任何子类上使用`extends`?

Vue3 Uncaught SyntaxError:每当我重新加载页面时,浏览器控制台中会出现无效或意外的令牌

类型';字符串|数字';不可分配给类型';未定义';.类型';字符串';不可分配给类型';未定义';

接受字符串或数字的排序函数

有没有什么方法可以使用ProvideState()提供多个削减器作为根减减器

Vite+Chrome扩展 list v3-;不能在模块外使用import语句;对于inpage脚本

Angular 16:无法覆盖;baseUrl;在tsconfig.app.json中

Typescript 从泛型推断类型

在 next.js 13.4 中为react-email-editor使用forwardRef和next/dynamic的正确方法

Typescript 编译错误:TS2339:类型string上不存在属性props