简体   繁体   中英

Angular2: Component doesn't update

I am building an angular application about some projects I did. I have a detail page that shows the detail of a project.

The problem: When I go to the project detail page, everything is correct. But when I switch the project variable (by using the navigation I have on the page) the dependency component doesn't update, they don't get the correct project ID.

范例图片

项目结构

The projectdetail.html page that contains all the components:

<div class="container-content" *ngIf="project">
    <div class="projectdetail">
        <div class="thumbnail header">
            <img [src]="project.img" [alt]="project.name" class="projectImage" />
            <div>
                <h2>{{project.name}}</h2>
                <!--The programming component that you can see in the image-->
                <programming-languages [ids]="project.languages"></programming-languages>
            </div>
        </div>
        <div>
            <carousel interval="5000" noWrap="false">
                <slide *ngFor="let slidez of slides; let index=index">
                    <img [src]="slidez.image" class="carouselimg" />
                    <div class="carousel-caption">
                        <h4>Slide {{slidez.id}}</h4>
                        <p>{{slidez.text}}</p>
                    </div>
                </slide>
             </carousel>
        </div>
        <div class="thumbnail detail">
            <p>{{project.description}}</p>
        </div>
    </div>
    <div class="projects hidden-mm hidden-sm hidden-xs">
        <projectsidelist [notShowId]="selectedProjectId"></projectsidelist>
    </div>
</div>

The language component code:

import { Component, Input, OnInit } from '@angular/core';

import { ProgrammingLanguagesService } from '../services/programminglanguages.service';
import { ProgrammingLanguage } from '../models/programmingLanguage';

@Component({
    selector: 'programming-languages',
    templateUrl: 'app/components/programminglanguages.component.html',
    providers: [ProgrammingLanguagesService]  
})
export class ProgrammingLanguages implements OnInit {
    @Input()
    ids: number[];
    programmingLanguages: ProgrammingLanguage[];
    constructor(private programmingLanguagesService: ProgrammingLanguagesService) {

    }

    getProgrammingLanguages() {
        this.programmingLanguagesService.getProgrammingLanguages().subscribe(programmingLanguages => {
            var result: ProgrammingLanguage[] = [];
            this.ids.forEach((key: number) => {
                programmingLanguages.forEach((programmingLanguage: ProgrammingLanguage) => {
                    if (programmingLanguage.id == key)
                    {
                        result.push(programmingLanguage);
                    }
                })
            })       
            this.programmingLanguages = result;
        });
    }

    ngOnInit() {
        setTimeout(() => this.getProgrammingLanguages(), 0);
    }
}

The programming language html:

<div class="languageoverlay">
    <span *ngFor="let programmingLanguage of programmingLanguages; let lastElement = last">
        <img [src]="programmingLanguage.img" [alt]="programmingLanguage.name" />
        <span [hidden]="lastElement" class="languageseperator">+</span>
    </span>
</div>

If you need extra code please ask or refer to the github

Github containing All the code

Explanation when you run the github: go to http://localhost:3000/#/projectdetail/5 and click another project on the left bar. Then you see the issue!

You were correctly passing an array of id's to the component but you never listen to the changes made so there is a quick fix for you:

in programminglanguages.component.ts change this code:

import { Component, Input, OnInit } from '@angular/core';

to

import { Component, Input, OnInit, OnChanges} from '@angular/core';

and

export class ProgrammingLanguages implements OnInit {

to

export class ProgrammingLanguages implements OnInit, OnChanges {

and finally add this lifecycle function to you class body:

 ngOnChanges(): void {
        this.getProgrammingLanguages();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM