例如,在这段代码中,我能够收集在initialState
对象中传递的数据:
const initialState = {
titulo: titulo,
origen: origen,
th1: "Número",
th2: "Nombres",
modalFor: "Locales"
};
this.bsModalRef = this.modalService.show(ClienteOlocalPopUpComponent, {
initialState,
backdrop: "static",
keyboard: false
});
并在ClienteOlocalPopUpComponent.html中插入其属性。
但是,如果我希望传递到const initialState
中的模态的值成为,例如,在ClienteOlocalPopUpComponent.ts的ngOnInit()
方法中在控制台中打印出来?我该如何实现?模态的.ts文件中无法识别它。
谢谢。
initialState
会覆盖模态组件属性,这就是为什么这些属性首先插在组件.html中的原因,因此可以安全地在组件.ts文件中使用这些值。假设您的.ts组件类似于:@Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.scss']
})
export class ModalComponent {
titulo;
origen;
th1;
th2;
modalFor;
....
}
如果显示类似模态的话>
const initialState = { titulo: titulo, origen: origen, th1: "Número", th2: "Nombres", modalFor: "Locales" }; this.bsModalRef = this.modalService.show(ModalComponent , { initialState, backdrop: "static", keyboard: false });
那么就可以了
ngOnInit() { console.log(this.origen); }
它将按预期工作。