Angular 等待方法完成,然后再设置值
我有一个正在调用的方法。在方法完成并得到响应之后,我想给变量赋值。目前我不确定如何在设置值之前等待端点调用完成 这是我的密码 成分ngOninit //在上述行完成后,我只应继续下面的操作 除了使用设置时间,还有其他方法吗Angular 等待方法完成,然后再设置值,angular,Angular,我有一个正在调用的方法。在方法完成并得到响应之后,我想给变量赋值。目前我不确定如何在设置值之前等待端点调用完成 这是我的密码 成分ngOninit //在上述行完成后,我只应继续下面的操作 除了使用设置时间,还有其他方法吗 public cacheDropdownOptions() { this._subscriptions.push(this.getRoles().subscribe((resp) => { if (resp) { const ro
public cacheDropdownOptions() {
this._subscriptions.push(this.getRoles().subscribe((resp) => {
if (resp) {
const rolesArr = Object.entries(resp).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('roles', JSON.stringify(rolesArr));
} else {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}));
const medicalPlansArr = medicalAidPlans.map(x => ({ label: x.name, value: x.id }));
this._subscriptions.push(this.getLookups().subscribe((resp) => {
if (resp) {
localStorage.setItem('medicalPlans', JSON.stringify(medicalPlansArr));
if (resp.genders) {
const genderArr = Object.entries(resp.genders).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('genders', JSON.stringify(genderArr));
}
if (resp.races) {
const racesArr = Object.entries(resp.races).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('races', JSON.stringify(racesArr));
}
if (resp.titles) {
const titlesArr = Object.entries(resp.titles).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('titles', JSON.stringify(titlesArr));
}
if (resp.maritalStatus) {
const maritalStatusArr = Object.entries(resp.maritalStatus).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('maritalStatus', JSON.stringify(maritalStatusArr));
}
if (resp.nationality) {
const nationalityArr = Object.entries(resp.nationality).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('nationality', JSON.stringify(nationalityArr));
}
if (resp.identityTypes) {
const identityTypesArr = Object.entries(resp.identityTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('identityTypes', JSON.stringify(identityTypesArr));
}
if (resp.companyTypes) {
const companyTypeArray = Object.entries(resp.companyTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('companyTypes', JSON.stringify(companyTypeArray));
}
if (resp.industries) {
const industryArray = Object.entries(resp.industries).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('industry', JSON.stringify(industryArray));
}
if (resp.fileTypes) {
const fileTypesArr = Object.entries(resp.fileTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('fileTypes', JSON.stringify(fileTypesArr));
}
if (resp.userDocumentCategories) {
const documentCategoriesArray = Object.entries(resp.userDocumentCategories).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('documentCategories', JSON.stringify(documentCategoriesArray));
}
if (resp.reasonExtend) {
const reasonExtendArr = Object.entries(resp.reasonExtend).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('extendReasons', JSON.stringify(reasonExtendArr));
}
if (resp.reasonRescind) {
const reasonRescindArr = Object.entries(resp.reasonRescind).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('rescindReasons', JSON.stringify(reasonRescindArr));
}
if (resp.sources) {
const savingsSourcesArr = Object.entries(resp.sources).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('savingsSources', JSON.stringify(savingsSourcesArr));
}
localStorage.setItem('contentSet', 'true');
this.lookupsCached.next(true);
} else {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}));
}
您可以将基本承诺与async/await一起使用
async ngOnInit() {
await this.cacheDropdownOptions();
this.nationalities = JSON.parse(localStorage.getItem('nationality'));
this.titles = JSON.parse(localStorage.getItem('titles'));
this.idDocumentTypes = JSON.parse(localStorage.getItem('identityTypes'));
this.genders = JSON.parse(localStorage.getItem('genders'));
}
async cacheDropdownOptions() {
.. use subscription
}
}
发布您的cacheDropdownOptions函数代码如果该方法返回一个承诺,您可以只使用承诺链来确保它在之后发生。
public cacheDropdownOptions() {
this._subscriptions.push(this.getRoles().subscribe((resp) => {
if (resp) {
const rolesArr = Object.entries(resp).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('roles', JSON.stringify(rolesArr));
} else {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the roles.');
}));
const medicalPlansArr = medicalAidPlans.map(x => ({ label: x.name, value: x.id }));
this._subscriptions.push(this.getLookups().subscribe((resp) => {
if (resp) {
localStorage.setItem('medicalPlans', JSON.stringify(medicalPlansArr));
if (resp.genders) {
const genderArr = Object.entries(resp.genders).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('genders', JSON.stringify(genderArr));
}
if (resp.races) {
const racesArr = Object.entries(resp.races).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('races', JSON.stringify(racesArr));
}
if (resp.titles) {
const titlesArr = Object.entries(resp.titles).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('titles', JSON.stringify(titlesArr));
}
if (resp.maritalStatus) {
const maritalStatusArr = Object.entries(resp.maritalStatus).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('maritalStatus', JSON.stringify(maritalStatusArr));
}
if (resp.nationality) {
const nationalityArr = Object.entries(resp.nationality).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('nationality', JSON.stringify(nationalityArr));
}
if (resp.identityTypes) {
const identityTypesArr = Object.entries(resp.identityTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('identityTypes', JSON.stringify(identityTypesArr));
}
if (resp.companyTypes) {
const companyTypeArray = Object.entries(resp.companyTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('companyTypes', JSON.stringify(companyTypeArray));
}
if (resp.industries) {
const industryArray = Object.entries(resp.industries).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('industry', JSON.stringify(industryArray));
}
if (resp.fileTypes) {
const fileTypesArr = Object.entries(resp.fileTypes).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('fileTypes', JSON.stringify(fileTypesArr));
}
if (resp.userDocumentCategories) {
const documentCategoriesArray = Object.entries(resp.userDocumentCategories).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('documentCategories', JSON.stringify(documentCategoriesArray));
}
if (resp.reasonExtend) {
const reasonExtendArr = Object.entries(resp.reasonExtend).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('extendReasons', JSON.stringify(reasonExtendArr));
}
if (resp.reasonRescind) {
const reasonRescindArr = Object.entries(resp.reasonRescind).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('rescindReasons', JSON.stringify(reasonRescindArr));
}
if (resp.sources) {
const savingsSourcesArr = Object.entries(resp.sources).map(x => ({ value: x[0], label: x[1].toString() }));
localStorage.setItem('savingsSources', JSON.stringify(savingsSourcesArr));
}
localStorage.setItem('contentSet', 'true');
this.lookupsCached.next(true);
} else {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}
}, (error) => {
this._errorService.openErrorPopup('An error occured while extracting the lookups.');
}));
}
async ngOnInit() {
await this.cacheDropdownOptions();
this.nationalities = JSON.parse(localStorage.getItem('nationality'));
this.titles = JSON.parse(localStorage.getItem('titles'));
this.idDocumentTypes = JSON.parse(localStorage.getItem('identityTypes'));
this.genders = JSON.parse(localStorage.getItem('genders'));
}
async cacheDropdownOptions() {
.. use subscription
}
}