[英]ngFor does not render elements into IOS browsers
I'm trying to learn Angular and followed the example described here . 我正在尝试学习Angular,并按照此处描述的示例进行操作。 It works fine into my desktop (macOS 10.13.3 / Safari 11.0.3; Chrome 64.0).
它可以在我的桌面上正常运行(macOS 10.13.3 / Safari 11.0.3; Chrome 64.0)。
However, when I access it through an iphone (ios 11.2.5 / Chrome 64.0) -- I started it using ng serve --host 0.0.0.0 --, the page does not render completely. 但是,当我通过iphone(iOS 11.2.5 / Chrome 64.0)访问它时(我使用ng serve --host 0.0.0.0启动它),页面无法完全呈现。
I modified the example and added two headers, as follows. 我修改了示例,并添加了两个标题,如下所示。 As you can see into the image, BOF and EOF are rendered, but the div inside it aren't.
如您所见,已渲染了BOF和EOF,但未渲染其中的div。
Searching, I found that I should enable polyfills. 搜索时,我发现应该启用polyfills。 I followed examples from here .
我从这里开始跟随例子。 I also modified
main.ts
and included import './polyfills.ts';
我还修改了
main.ts
并包含了import './polyfills.ts';
So my question is how do I configure angular to display content on both platforms (desktop/mobile)? 所以我的问题是我如何配置angular在两个平台(台式机/移动设备)上显示内容?
EDIT 编辑
Seems that the problem is related with the service that populates the cars
variable. 似乎该问题与填充
cars
变量的服务有关。 My component looks like: 我的组件看起来像:
export class CarListComponent implements OnInit {
cars: Array<any>;
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getAll().subscribe(data => {
this.cars = data;
});
}
}
If, instead calling the service, I manually populate the array, the list is properly rendered... 如果代替调用服务而是手动填充数组,则列表将正确呈现...
export const TEMP: any[] = [
{
name: 'Car 1'
}, {
name: 'Car 2'
}, {
name: 'Car 3'
}
];
And then 接着
ngOnInit() {
this.cars = TEMP;
}
The code will work on IOS Safari/Chorme. 该代码将在IOS Safari / Chorme上运行。 The problem is not with the service, because if I use
console.log(data)
, it shows that it is filled with valid information. 问题不在于服务,因为如果我使用
console.log(data)
,它表明它充满了有效信息。 I think the problem is related to Observable... 我认为问题与可观察...有关
<h2>BOF</h2>
<div *ngFor="let car of cars">
{{car.name}}
</div>
<h2>EOF</h2>
Versions: 版本:
Angular CLI: 1.6.7
Node: 8.9.4
OS: darwin x64
Angular: 5.2.4
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cdk: 5.2.1
@angular/cli: 1.6.7
@angular/flex-layout: 2.0.0-beta.12
@angular/material: 5.2.1
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.7
@schematics/angular: 0.1.17
typescript: 2.5.3
webpack: 3.10.0
Sounds like it might be a change detection issue. 听起来可能是更改检测问题。 Are you using
ChangeDetectionStrategy.OnPush
? 您正在使用
ChangeDetectionStrategy.OnPush
吗? If so, then you'd need to manually call ChangeDetectorRef
markForCheck()
after changes. 如果是这样,那么你需要手动调用
ChangeDetectorRef
markForCheck()
更改后。 Or you might try using the async
pipe, which would forgo the need for markForCheck()
或者您可以尝试使用
async
管道,这将不再需要markForCheck()
ngOnInit() {
this.cars = this.carService.getAll()
}
and 和
<h2>BOF</h2>
<div *ngFor="let car of cars | async">
{{car.name}}
</div>
<h2>EOF</h2>
I found the problem, and I'm ashamed... 我发现问题了,我很as愧...
My getAll()
method into car.service.ts
was pointing to "localhost"... I just changed it to an IP, and it worked.... 我将
getAll()
方法转换为car.service.ts
指向“ localhost” ...我只是将其更改为IP,并且它起作用了。
getAll(): Observable<any> {
return this.http.get('//10.10.10.10:8080/cars');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.