我正在学习独立组件的Angular 17,但我无法使用属性 Select 器来处理我的组件.

如文档中所解释的,我应该能够为我的组件https://angular.dev/guide/components/selectors#types-of-selectors使用属性

当我try 它时,我得到以下错误

无法绑定到‘app-list-Item’,因为它不是‘tr’的已知属性.

我试着制作了一个类型和类 Select 器,它们的工作情况与预期一致. 使用属性 Select 器时.我收到一个错误.

这是我的Angular 组件. 可以在这里找到问题的堆积如山:https://stackblitz.com/edit/angular-17-starter-project-4t9qcr?file=src%2Flist.component.ts

list-item-component.ts

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-list-item, .app-list-item, [app-list-item]',
  standalone: true,
  imports: [],
  template: `         
          <td>{{ item.A }}</td>
          <td>{{ item.B }}</td> 
  `,
  styles: ``,
})
export class ListItemComponent {
  @Input() item!: { A: string; B: string };
}

list.component.ts

import { Component } from '@angular/core';
import { ListItemComponent } from './list-item.component';

@Component({
  selector: 'app-list',
  standalone: true,
  template: `
    <div> 
      <h3>Type selector</h3>
        <table> 
          <tr>
            <th>A</th>
            <th>B</th>
          </tr>
          @for (item of data; track $index){
            <tr>
            <app-list-item [item]="item"/>  
          </tr>
          }
        </table> 

        <h3>class selector</h3>
        <table> 
          <tr>
            <th>A</th>
            <th>B</th>
          </tr>
          @for (item of data; track $index){
            <tr class="app-list-item" [item]="item"></tr>
            
          }
        </table> 

        <h3>Attribute selector</h3>
        <table> 
          <tr>
            <th>A</th>
            <th>B</th>
          </tr>
          @for (item of data; track $index){
            <tr [app-list-item] [item]="item"></tr>
          }
        </table> 
    </div>
  `,
  styles: ``,

  imports: [ListItemComponent],
})
export class ListComponent {
  data = [
    { A: 'a1', B: 'b1' },
    { A: 'a2', B: 'b2' },
  ];
}

当您删除<tr [app-list-item] [item]="item"></tr>时,它会按预期工作,但我更喜欢有属性的解决方案.

推荐答案

应该是这样的

<tr app-list-item [item]="item"></tr>

Angular相关问答推荐

angular:从模板中的可观察数组获取随机元素

为什么当我在父组件中设置数组元素时,Angular重新创建子组件而不是更新它?

如何在滑动滑块中获取当前项目的索引?

NGNEW不会在src根文件夹ANGLI CLI 17.0.10中生成app.mode.ts

如何使用formBuilder.Group()从角形表单中删除选定的选项?

所有返回缺少权限或权限不足的FireBase项目.

一次重定向后,所有子路由都处于活动状态

Angular 16 Auth Guard在具有0的服务时给出Null Injector错误;httpclient;被注入

无法在 Angular 中使用 CryptoJS 正确加密

如果我使用 matAutocomplete,为什么 textarea 不显示 formControl 的实际值

Angular combinelatest使用没有初始值的主题

Angular 4 - 在下拉列表中 Select 默认值 [Reactive Forms]

CORS 错误:requests are only supported for protocol schemes: http…等

带有 Angular 的 Font Awesome 5

在Angular 4中以'yyyy-MM-dd'格式获取当前日期

angular4的所见即所得编辑器

如何每隔一段时间发出 HTTP 请求?

如何在项目中导入 Angular material?

在 Angular 2 中对 observable 进行单元测试

Angular2:将数组转换为 Observable