json - Observable 和 http json Angular 的问题

标签 json angular http observable

我有一个问题,因为我从来没有在 * nFor 中绘制变量“datos”, 我认为问题出在渲染上 因此实现了“Observable”,但它仍然不起作用

感谢您的帮助!

“标题菜单.component.ts”

import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
    import { Http } from '@angular/http';
    import 'rxjs/add/operator/map';
    import 'rxjs/add/operator/catch';
    import { Observable } from 'rxjs/Observable';

    @Component({
        moduleId: module.id,
        selector: 'as-headermenu',
        templateUrl: 'header-menu.html',
        changeDetection: ChangeDetectionStrategy.OnPush
    })

    export class HeaderMenuComponent implements OnInit {
    datos = []; // Aqui se carga el json
    private ruta = 'assets/json_header_menu.json';  

    constructor(private http: Http) {}

    ngOnInit() {
         this.loadJson().subscribe(
                    vdatos => this.datos = vdatos,
                    err => console.log(err),
        () => console.log(this.datos));
        console.log(this.datos);
    }

loadJson(): Observable<any[]> {
          return this.http.get(this.ruta)
            .map(res => res.json());
}
}

“标题菜单.html”

<ul *ngIf="datos"> <li *ngFor="let dato of datos | async; let i =
index"> {{ dato.url }} {{ dato.icono }} {{ dato.titulo }} {{ i }} 
</li> 

“标题菜单模块.ts”

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule } from '@angular/http';

import { HeaderMenuComponent } from './index';

@NgModule({
    declarations: [
        HeaderMenuComponent
    ],
    imports: [
        RouterModule,
        BrowserModule,
        HttpModule
    ],
    exports: [
        HeaderMenuComponent
    ]
})
export class HeaderMenuModule {
}

图像控制台错误:Console Error

最佳答案

这里不需要异步管道,没有它也应该可以工作,

<ul *ngIf="datos"> <li *ngFor="let dato of datos; let i =
index"> {{ dato.url }} {{ dato.icono }} {{ dato.titulo }} {{ i }} 
</li> 

关于json - Observable 和 http json Angular 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44724854/

相关文章:

angular - 使用angular提交后如何获取所选 ionic 单选按钮的值

java - 在 Swing Worker 类中不适用于循环

ios - 将 JSON 导入 Realm.io

python - 如何从 django 模板在 html 页面上打印漂亮的 JSON?

javascript - 如何在 Angular 2 中使用 Angular 1 指令

angular - Angular 2 http官方教程中的私有(private)变量命名约定

HTTP 规范 : Proxy-Authorization and Authorization headers

http - 在同一个 URL 上运行 SOAP 和 RESTful

c# - 如何使用 System.Text.Json 解析带注释的 JSON?

javascript - 不和谐 : Using a command to get Data from Array