首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在模板中列出对象?

如何在模板中列出对象?
EN

Stack Overflow用户
提问于 2017-03-17 19:59:07
回答 1查看 1.3K关注 0票数 0

我想列出一些我在服务中已经拥有的对象,但是当我加载本地主机时,它们不会出现。以下是代码:

item.model.ts

代码语言:javascript
复制
export class Item {
  status: string = 'started';
  constructor(public title: string = ''){}
}

app.component.ts

代码语言:javascript
复制
import { Component, Inject } from '@angular/core';

import { Item }   from './app.model';
import { AppService }from './app.service';

@Component({
  selector: 'app-root',
  template: `
   <div>
     <form (submit)="onSubmit()">
       <input type="text" [(ngModel)]="item.title" name="title">
     </form>
     <app-list></app-list>
   </div>
  `
})

export class AppComponent{
 item: Item = new Item();

 constructor(public appService: AppService){
 }

 onSubmit(){
  this.appService.todos.push(this.item);
  console.log('return: ' + this.appService.todos);
  this.item = new Item();
 }

}

app-list.component.ts

在这个组件中,我试着列出在HTML.中可以看到的

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { AppService }from './app.service';

import { Item } from './app.model';

@Component({
  selector: 'app-list',
  template: 
  `
 <div>
  <ul>
   <li *ngFor="let todo of todos">
    <span>{{todo.title}}</span>
   </li>
  </ul>
  </div>
  `
})

export class AppList implements OnInit {
 todo: Item[];

 constructor(private appService: AppService){}

 ngOnInit(): void {
  let todos = this.appService.todos;
  console.log('todos: ' + todos);
 }

}

app.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Item }       from './app.model';

@Injectable()
export class AppService {
    todos = [
        {'id' : 1, 'description': "test item 1"},
        {'id' : 2, 'description': "test item 2"}
    ];
}

当我添加一个新项时,我不会看到任何东西,但是控制台显示:

todos: [object Object],[object Object],[object Object]

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-17 21:10:55

感谢@BarbarBilal,我发现了错误,我在这里发布给你们看。一切都如我所愿。

首先,模式:

代码语言:javascript
复制
export class Item {
 constructor(public title: string = ''){}
}

然后,AppComponent:

代码语言:javascript
复制
import { Component, Inject } from '@angular/core';

import { Item }   from './app.model';
import { AppService }from './app.service';

@Component({
  selector: 'app-root',
  template: `
   <div>
  <form (submit)="onSubmit()">
   <input type="text" [(ngModel)]="item.title" name="title">
  </form>
  <app-list></app-list>
 </div>
  `
})

export class AppComponent{

 item: Item = new Item();

 constructor(public appService: AppService){
 }

 onSubmit(){
  this.appService.todos.push(this.item);
  console.log('return: ' + this.appService.todos[0].title);
  this.item = new Item();
 }

}

AppList:

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { AppService }from './app.service';

import { Item } from './app.model';

@Component({
  selector: 'app-list',
  template: 
  `
 <div>
  <ul>
   <li *ngFor="let todo of todos">
    {{todo.title}}
   </li>
  </ul>
  </div>
  `
})

export class AppList implements OnInit{

 todos: Item[];

 constructor(private appService: AppService){}

 ngOnInit(): void {
  this.todos = this.appService.todos;
  console.log('todos: ' + this.todos[0].title);
 }
}

最后,AppService:

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Item }       from './app.model';

@Injectable()
export class AppService {

    todos = [
        {title : 'test item 1'}
    ];

}

如您所见,AppService现在有todo和title属性。应用程序列表组件现在有待办事项。我在AppList组件中创建了todos变量全局。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42866137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档