【问题标题】:Angular2 @input pass the array objectAngular2 @input 传递数组对象
【发布时间】:2017-07-31 13:42:09
【问题描述】:

我的简单 Angular 待办事项应用程序中有两个组件

tod​​o-root.ts

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

export class TodoItem {
  todo: String;
}

@Component({
  selector: 'todo-root',
  template: `
    <div class="todo-root">
      <todo-list [todoList]="todoTaskList"></todo-list>
      <todo-add></todo-add>
    </div>
  `
})
export class TodoRootComponent {
    todoTaskList= [
        {todo: 'todo1'},
        {todo: 'todo2'},
    ]
}

待办事项列表.ts

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

export class TodoItem {
  todo: String;
}

@Component({
  selector: 'todo-list',
  template: `
    <div *ngFor="let todoTask of todoList">
        <span>{{todoTask.todo}}</span>
    </div>
  `
})
export class TodoListComponent {

    @Input()
    todoList:TodoItem[] = [];
}

如何在“todo-list”组件 ngFor 循环中获取“todoTaskList”值。如何@input 在这种情况下工作。谁能帮忙。

【问题讨论】:

  • 它应该可以正常工作,你可以创建一个 plunker 吗?

标签: angular


【解决方案1】:

替换此行

@Input() todoList:TodoItem[] = [];

@Input() todoList:TodoItem[];

【讨论】:

  • 为什么?您在构造函数中定义的内容无效,因为它将被输入更新覆盖
猜你喜欢
  • 2016-12-16
  • 1970-01-01
  • 2016-07-22
  • 2018-07-15
  • 2016-10-24
  • 2023-03-30
  • 2018-07-11
  • 2017-05-08
  • 2021-06-10
相关资源
最近更新 更多