Javascript 角度4初始子组件输入
My parent component html包含以下行,用于调用默认值为maxPrice的子组件:Javascript 角度4初始子组件输入,javascript,html,angular,Javascript,Html,Angular,My parent component html包含以下行,用于调用默认值为maxPrice的子组件: 只需使用*ngIf: <app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events> 或 <ng-container *ngIf="_maxPrice"> <app-filter-events [maxPrice]='_maxPrice'&g
只需使用*ngIf
:
<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>
或
<ng-container *ngIf="_maxPrice">
<app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</ng-container>
只需使用
*ngIf
:
<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>
或
<ng-container *ngIf="_maxPrice">
<app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</ng-container>
在
\u maxPrice
尚未定义时,您无法实例化应用程序筛选器事件
组件:
<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>
在
\u maxPrice
尚未定义时,您无法实例化应用程序筛选器事件
组件:
<app-filter-events *ngIf='_maxPrice' [maxPrice]='_maxPrice'></app-filter-events>
很简单,在调用调用前将变量设置为false,在异步完成后将其设置为true。您不希望在完成之前将组件加载到dom中
<div *ngIf="maxPriceBoolean">
<app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</div>
很简单,在调用调用前将变量设置为false,在异步完成后将其设置为true。您不希望在完成之前将组件加载到dom中
<div *ngIf="maxPriceBoolean">
<app-filter-events [maxPrice]='_maxPrice'></app-filter-events>
</div>
构造函数
在@Input()
之前执行,因此绑定输入值在构造函数块中未定义,请使组件实现OnInit并访问ngOnInit()
方法中的@Input()变量,即可使用。构造函数
在@Input()
之前执行,因此,在构造函数块中未定义绑定输入值,使组件实现OnInit并在ngOnInit()
方法中访问@Input()变量,即可使用。使用异步管道:
<app-filter-events [maxPrice]='_maxPrice$ | async'></app-filter-events>
组件代码
_maxPrice$: Observable<number>;
constructor(private _dataService: DataService) {
this._maxPrice$ = this._dataService.getEventsByCriteria(this._filterCriteria)
}
\u maxPrice$:可观察;
构造函数(专用数据服务:数据服务){
this.\u maxPrice$=this.\u dataService.getEventsByCriteria(this.\u filterCriteria)
}
使用异步管道:
<app-filter-events [maxPrice]='_maxPrice$ | async'></app-filter-events>
组件代码
_maxPrice$: Observable<number>;
constructor(private _dataService: DataService) {
this._maxPrice$ = this._dataService.getEventsByCriteria(this._filterCriteria)
}
\u maxPrice$:可观察;
构造函数(专用数据服务:数据服务){
this.\u maxPrice$=this.\u dataService.getEventsByCriteria(this.\u filterCriteria)
}
wat是_maxPrice的类型?尝试实现NgOnInit接口。wat是_maxPrice的类型?尝试实现NgOnInit接口。我需要有maxPrice,我认为在获得maxPrice之前子组件已实例化。如何在实例化子组件之前等待maxPrice?“在实例化子组件之前等待maxPrice”=>这正是此代码中的*ngIf需要的maxPrice,我认为子组件在获得maxPrice之前已实例化。如何在实例化子组件之前等待maxPrice?“在实例化子组件之前等待maxPrice”=>这正是此代码中的*ngIf需要的maxPrice,我认为子组件在获得maxPrice之前已实例化。如何在实例化子组件之前等待maxPrice?zak,这将做同样的事情这将检查maxPrice是否实例化,如果是,它将加载组件,否则不会。@zak,很高兴知道它帮助了你,请你也接受答案?我需要maxPrice,我认为子组件是在得到maxPrice之前实例化的。如何在实例化子组件之前等待maxPrice?zak,这将做同样的事情这将检查maxPrice是否实例化,如果是,它将加载组件,否则不会。@zak,很高兴知道它帮助了你,请你也接受答案吗?@zac请尝试一下。不需要添加任何*ngify您的答案看起来很优雅,但我需要maxPrice是数字,而不是可观察的。感谢you@zac请试试这个。不需要添加任何*ngify您的答案看起来很优雅,但我需要maxPrice是数字,而不是可观察的。非常感谢。