我在 Angular 中使用 intl-tel-input 插件。我有包含 FormGroupphoneNumber
和countryCode
属性的父组件。我将这些属性作为表单控件传递给孩子。子组件正在使用 intl-tel-input 插件,代码如下所示
ngAfterViewInit(): void {
this.iti = intlTelInput(this.input.input.nativeElement, {
utilsScript: '../../assets/js/phone-utils.js',
preferredCountries: this.preferredCountries
})
this.input.nativeElement.addEventListener('countrychange', () => {
// set a value in the parent
this.countryCodeControl.setValue(this.iti.getSelectedCountryData().dialCode)
})
}
我希望countrychange
在开始时触发事件,以便我可以将countryCode
父表单组中的 设置为列表中的第一个。我列表中的第一个国家是德国,如果我this.iti.setCountry('de')
在初始化后添加,则不会触发该事件,因为该国家已在下拉列表中选择。我必须做这样的事情
this.iti.setCountry('at') // change the initial country
this.iti.setCountry('de') // set the initial country once again
我的问题是,countrychange
即使我试图设置已经选择的国家,我能以某种方式触发事件吗?