2

I have a problem because I never draw the variable "datos" in the * nFor, I think the problem is in the render, for this reason implements "Observable", however it still does not work

Thanks for help!

"header-menu.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());
}
}

"header-menu.html"

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

"header-menu-module.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 {
}

Image Console error:Console Error

2
  • 1
    try removing the async pipe (| async) Commented Jun 23, 2017 at 15:17
  • As addition, that you should indeed remove the async pipe (or don't use subscribe) Seems from your error message where you have console logged your response (I assume) and it shows [ ] so it would be empty? Commented Jun 23, 2017 at 15:40

3 Answers 3

1

You don't need the async pipe here, it should work without it,

<ul *ngIf="datos"> <li *ngFor="let dato of datos; let i =
index"> {{ dato.url }} {{ dato.icono }} {{ dato.titulo }} {{ i }} 
</li> 
Sign up to request clarification or add additional context in comments.

1 Comment

just use ngOnInit() { this.datos = this.loadJson(); }
1

| async pipe does help to unwrap your data from Observable stream. So to make your code work you should assign observable to datos variable.

ngOnInit() {
    this.datos = this.loadJson();
}

2 Comments

When i change datos to: datos: Observable<any[]>; and assign this.datos = this.loadJson().subscribe( vdatos => this.datos = vdatos, err => console.log(err), () => console.log(this.datos)); <br> I get the error : Type 'Subscription' is not assignable to type 'Observable<any[]> please help Mr. Pankaj Parkar
@SantiagoMatiz check my answer.. It should not have part after .subscribe.. ngOnInit will only have this.datos = this.loadJson();
0

I fix it removing the line of code "changeDetection"

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

thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.