【发布时间】:2019-07-28 23:37:05
【问题描述】:
我在 Angular 工作,在哪里 -
-
我正在尝试在值更改时更新子组件中的值 在父组件中
(因为值是从其他组件动态传递到父组件的) .
我是如何尝试的
我尝试将数据从父组件传递到子组件使用 @Input 装饰器
当组件加载和 后面的值没有通过
我在下面分享我的代码
父组件
.html
<app-banner [tournamentType]='tournamentType'></app-banner>
.ts
子组件
.ts 文件
import { Component, OnInit , Input } from '@angular/core';
import { ServicesService } from '../service/services.service';
@Component({
selector: 'app-banner',
templateUrl: './banner.component.html',
styleUrls: ['./banner.component.scss']
})
export class BannerComponent implements OnInit {
@Input() tournamentType;
sportsType : any = 1;
constructor(private rest : ServicesService) { }
ngOnInit() {
console.log("this. is banner page" + this.tournamentType);
alert('hello');
this.loadDataFromApi(1);
}
loadDataFromApi(sportsType) {
this.rest.getbanner(this.sportsType).then(res => {
console.log('>>>$$$$$ banner >>>>>> $$$$$$$$$$');
console.log(' @Input tournamentType; ====' + this.tournamentType );
console.log(res);
})
console.log(sportsType);
}
}
【问题讨论】:
-
tournamentType是对象还是数组? -
分享您的 parent.componen.ts 代码