子组件中的角@Input也会更新父组件中的值

时间:2019-02-26 18:44:11

标签: angular property-binding

将属性上的@Input与[propertyNameInParent] =“ propertyNameInChild”组合使用,可以将父属性的值绑定到子属性。也称为单向绑定。不幸的是,就我而言,它也在更新父类中的属性。这怎么可能?

父项->最喜欢的城市 子组件->城市详细信息

因此,基本上,在(单击)事件上,将选择喜欢的城市(selectedCity属性)。然后,在城市详细信息组件中,单击onPlusIconClick()即可收到城市的赞。不知何故,这也触发了城市的发展。为什么我不必使用事件发射器将更新后的值发送回父组件?

favorite-cities.component.ts

import {Component, OnInit} from '@angular/core';
import {CitiesService} from '../../services/cities.service';
import {City} from '../../model/city';

@Component({
selector: 'app-favorite-cities',
templateUrl: './favorite-cities.component.html',
styleUrls: ['./favorite-cities.component.css']
})
export class FavoriteCitiesComponent implements OnInit {

cities: City[];
selectedCity: City;

constructor(private citiesService: CitiesService) {
}

ngOnInit() {
    this.citiesService.getCities().subscribe(
        (data) => this.cities = data,
        (err) => console.log(err),
        () => { }
    );
}

setSelectedCity(city: City): void {
    this.selectedCity = city;
}

}

favorite-cities.component.html

<div class="container-fluid">
    <div class="row justify-content-center">
        <div class="col-4">
            <h2>Mijn favoriete steden zijn: </h2>
            <ul class="list-group" *ngFor="let city of cities">
                <li class="list-group-item" (click)="setSelectedCity(city)">{{city.id}} - {{city.name}}
                    <span class="float-right badge badge-success">Likes {{city.rating}} .   </span>
                </li>
            </ul>
        </div>
        <div class="col-4">
            <app-city-detail [selectedCity]="selectedCity"></app-city-detail>
        </div>
    </div>
</div>

city-detail.component.ts

import {Component, Input, OnInit} from '@angular/core';
import {City} from '../../model/city';
import {CitiesService} from '../../services/cities.service';

@Component({
selector: 'app-city-detail',
templateUrl: './city-detail.component.html',
styleUrls: ['./city-detail.component.css']
})
export class CityDetailComponent implements OnInit {

@Input()
selectedCity: City;

constructor(private citiesService: CitiesService) {
}

ngOnInit() {
}

onMinusIconClick(): void {
    if (this.selectedCity.rating > 0) {
        this.selectedCity.rating -= 1;
    }
    this.citiesService.updateRatingOfCity(this.selectedCity).subscribe(
        (data) => { console.log('oaksdoaks'); },
    );

}

onPlusIconClick(): void {
    this.selectedCity.rating += 1;
    this.citiesService.updateRatingOfCity(this.selectedCity).subscribe();
}
}

city-detail.component.html

<div *ngIf="selectedCity">
<div class="container-fluid">
    <div class="row">
        <h2>City details</h2>
        <div class="col-1">
            <i (click)="onPlusIconClick()" class="fas fa-2x fa-plus-square"></i>
        </div>
        <div class="col-1">
            <i (click)="onMinusIconClick()" class="fas fa-2x fa-minus-square"></i>
        </div>
    </div>
</div>
<ul class="list-group">
    <li class="list-group-item">Naam: {{selectedCity.id}} </li>
    <li class="list-group-item">Provincie: {{selectedCity.provincie}}</li>
    <li class="list-group-item">Highlights: {{selectedCity.highlights}} </li>
</ul>
<img class="img-fluid" src="assets/img/{{selectedCity.name.trim()}}.jpg" >

1 个答案:

答案 0 :(得分:4)

因为您正在输入一个对象,所以这仅仅是一个reference。因此,父母和孩子都有一个引用完全相同的对象的属性。

如果这是不必要的行为,则在设置所选城市时必须创建浅表副本。或在您更新的任何其他地方。有很多选择:

setSelectedCity(city: City): void {
  this.selectedCity = { ...city }; //shallow copy
}