Angular 没有将对象传递到 html 页面
Angular not passing object to html page
我在学习方面遇到问题 angular,我以前没有遇到过这些问题,但现在当我做这些演示时一切正常,直到我不再使用直接在 * 中定义的对象.component.ts 文件从其他地方拉取对象。在 2 个不同的实例中使用 2 个不同的机制(微服务,直接服务对象的服务组件)。这个特别的例子就是最好的例子。下面的代码工作正常,但下面的代码不会将卡片对象传递到页面。调试显示正在填充对象,但是当它到达 HTML 页面时它是未定义的。
import { Component } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { AppService } from '../app.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
/** Based on the screen size, switch from standard to one column per row */
//cards = [];
cardsForHandset = [];
cardsForWeb = [];
//isHandset: boolean = false;
cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map(({ matches }) => {
if (matches) {
return [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 2, rows: 1 },
{ title: 'Card 3', cols: 2, rows: 1 },
{ title: 'Card 4', cols: 2, rows: 1 }
];
}
return [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 1, rows: 1 },
{ title: 'Card 3', cols: 1, rows: 2 },
{ title: 'Card 4', cols: 1, rows: 1 }
];
})
);
constructor(private breakpointObserver: BreakpointObserver,
public appService: AppService,
) { }
}
HTML
<div class="grid-container">
<h1 class="mat-h1">Todays Deals</h1>
<mat-grid-list cols="2" rowHeight="350px">
<mat-grid-tile *ngFor="let card of cards | async" [colspan]="card.cols" [rowspan]="card.rows">
<mat-card class="dashboard-card">
<mat-card-header>
<mat-card-title>
{{card.title}}
</mat-card-title>
</mat-card-header>
</mat-card>
</mat-grid-tile>
</mat-grid-list>
</div>
这是无效的代码。
import { Component } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { AppService } from '../app.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
/** Based on the screen size, switch from standard to one column per row */
cards = [];
cardsForHandset = [];
cardsForWeb = [];
isHandset: boolean = false;
isHandsetObserver: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map(({ matches }) => {
if (matches) {
return true;
}
return false;
})
);
constructor(private breakpointObserver: BreakpointObserver,
public appService: AppService,
) { }
ngOnInit() {
this.isHandsetObserver.subscribe(currentObserverValue => {
this.isHandset = currentObserverValue;
this.loadCards();
this.cards.push();
});
this.appService.getDeals().subscribe(
response => {
this.cardsForHandset = response.handsetCards;
this.cardsForWeb = response.webCards;
this.loadCards();
},
error => {
// alert('There was an error in receiving data from server. Please come again later!');
}
);
}
loadCards() {
this.cards = this.isHandset ? this.cardsForHandset : this.cardsForWeb;
}
}
HTML - 与上面相同,但删除了异步。
服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AppService {
constructor(public httpClient:HttpClient) { }
getDeals(): Observable<any> {
return this.httpClient.get('http://localhost:3000/deals');
}
}
服务器端微服务
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
let jsaonResponse = {
"handsetCards": [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 2, rows: 1 },
{ title: 'Card 3', cols: 2, rows: 1 },
{ title: 'Card 4', cols: 2, rows: 1 }
],
"webCards": [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 1, rows: 1 },
{ title: 'Card 3', cols: 1, rows: 2 },
{ title: 'Card 4', cols: 1, rows: 1 }
]
}
res.json(jsaonResponse);
});
module.exports = router;
错误如下:
Error Message
这是打字稿打字错误。由于 cards
是一个数组,而您的 getDeals
returning 单个对象打字稿不知道该类型是什么。
一个好的解决方案是创建一个模型:
card.model.ts
:
export interface Cart {
title: string;
cols: number;
rows: number;
}
getDeals
in app.service.ts
会 return 一个 Card
的数组(不要忘记导入你的模型):
getDeals(): Observable<Card[]> {
return this.httpClient.get('http://localhost:3000/deals');
}
您在 home.component.ts
中的变量将如下所示:
cards: Card[] = [];
cardsForHandset: Card[] = [];
cardsForWeb: Card[] = [];
我在学习方面遇到问题 angular,我以前没有遇到过这些问题,但现在当我做这些演示时一切正常,直到我不再使用直接在 * 中定义的对象.component.ts 文件从其他地方拉取对象。在 2 个不同的实例中使用 2 个不同的机制(微服务,直接服务对象的服务组件)。这个特别的例子就是最好的例子。下面的代码工作正常,但下面的代码不会将卡片对象传递到页面。调试显示正在填充对象,但是当它到达 HTML 页面时它是未定义的。
import { Component } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { AppService } from '../app.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
/** Based on the screen size, switch from standard to one column per row */
//cards = [];
cardsForHandset = [];
cardsForWeb = [];
//isHandset: boolean = false;
cards = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map(({ matches }) => {
if (matches) {
return [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 2, rows: 1 },
{ title: 'Card 3', cols: 2, rows: 1 },
{ title: 'Card 4', cols: 2, rows: 1 }
];
}
return [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 1, rows: 1 },
{ title: 'Card 3', cols: 1, rows: 2 },
{ title: 'Card 4', cols: 1, rows: 1 }
];
})
);
constructor(private breakpointObserver: BreakpointObserver,
public appService: AppService,
) { }
}
HTML
<div class="grid-container">
<h1 class="mat-h1">Todays Deals</h1>
<mat-grid-list cols="2" rowHeight="350px">
<mat-grid-tile *ngFor="let card of cards | async" [colspan]="card.cols" [rowspan]="card.rows">
<mat-card class="dashboard-card">
<mat-card-header>
<mat-card-title>
{{card.title}}
</mat-card-title>
</mat-card-header>
</mat-card>
</mat-grid-tile>
</mat-grid-list>
</div>
这是无效的代码。
import { Component } from '@angular/core';
import { map } from 'rxjs/operators';
import { Breakpoints, BreakpointObserver } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { AppService } from '../app.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent {
/** Based on the screen size, switch from standard to one column per row */
cards = [];
cardsForHandset = [];
cardsForWeb = [];
isHandset: boolean = false;
isHandsetObserver: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset).pipe(
map(({ matches }) => {
if (matches) {
return true;
}
return false;
})
);
constructor(private breakpointObserver: BreakpointObserver,
public appService: AppService,
) { }
ngOnInit() {
this.isHandsetObserver.subscribe(currentObserverValue => {
this.isHandset = currentObserverValue;
this.loadCards();
this.cards.push();
});
this.appService.getDeals().subscribe(
response => {
this.cardsForHandset = response.handsetCards;
this.cardsForWeb = response.webCards;
this.loadCards();
},
error => {
// alert('There was an error in receiving data from server. Please come again later!');
}
);
}
loadCards() {
this.cards = this.isHandset ? this.cardsForHandset : this.cardsForWeb;
}
}
HTML - 与上面相同,但删除了异步。
服务
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AppService {
constructor(public httpClient:HttpClient) { }
getDeals(): Observable<any> {
return this.httpClient.get('http://localhost:3000/deals');
}
}
服务器端微服务
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
let jsaonResponse = {
"handsetCards": [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 2, rows: 1 },
{ title: 'Card 3', cols: 2, rows: 1 },
{ title: 'Card 4', cols: 2, rows: 1 }
],
"webCards": [
{ title: 'Card 1', cols: 2, rows: 1 },
{ title: 'Card 2', cols: 1, rows: 1 },
{ title: 'Card 3', cols: 1, rows: 2 },
{ title: 'Card 4', cols: 1, rows: 1 }
]
}
res.json(jsaonResponse);
});
module.exports = router;
错误如下:
Error Message
这是打字稿打字错误。由于 cards
是一个数组,而您的 getDeals
returning 单个对象打字稿不知道该类型是什么。
一个好的解决方案是创建一个模型:
card.model.ts
:
export interface Cart {
title: string;
cols: number;
rows: number;
}
getDeals
in app.service.ts
会 return 一个 Card
的数组(不要忘记导入你的模型):
getDeals(): Observable<Card[]> {
return this.httpClient.get('http://localhost:3000/deals');
}
您在 home.component.ts
中的变量将如下所示:
cards: Card[] = [];
cardsForHandset: Card[] = [];
cardsForWeb: Card[] = [];