大佬教程收集整理的这篇文章主要介绍了Angular 2 – 具有动态对象/属性的ngModel,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
private SELEctValsObj: any = {}; setSELEctedValsObj(sectionsArr) { sectionsArr.forEach(section => { section.questions.forEach(questionObj => { if (questionObj.type === 'drop-down') { this.SELEctValsObj[questionObj.questionId] = { SELEcted: questionObj.@R_397_5696@etails[0] }; } }) }); }
在我的HTML中,我想将输入中的[ngModel]绑定到SELEctValsObj对象上的属性.我试过这个,但没有运气:
<div *ngFor="let question of section.questions"> <div class="drop-down-question" *ngIf="question?.type === 'drop-down'"> <SELEct class="q-SELEct" [(ngModel)]="SELEctValsObj[questionId].SELEcted" // <== doesnt work either** // [(ngModel)]="SELEctValsObj[{{ questionId }}].SELEcted" // <== doesnt work** name="answerForQuestion{{ question?.questionId }}"> <option *ngFor="let answer of question?.@R_397_5696@etails" [ngValue]="answer"> {{ answer?.value }} </option> </SELEct> </div> </div>
> property SELEctValsObj声明为private,但您尝试在模板中使用它
>在模板中,您试图迭代section.questions但我没有看到它在其他地方定义,而是在每个局部范围的setSELEctedValsObj方法中定义
>由于缺少类型定义,您可能错误地使用了数据
interface QuestionModel { type: String; questionId: String; @R_397_5696@etails: String[]; } const mock_DATA = [ { questions: [{ type: 'drop-down',questionId: '42',@R_397_5696@etails: ['wololo'],}],},]; @Component(...) export class ProductsComponent { SELEctValsObj: { [key: String]: { SELEcted: String } } = {}; constructor() { this.setSELEctedValsObj(mock_DATA); } setSELEctedValsObj(sectionsArr: { questions: QuestionModel[] }[]) { sectionsArr.forEach(section => { section.questions.forEach(questionObj => { if (questionObj.type === 'drop-down') { this.SELEctValsObj[questionObj.questionId] = {SELEcted: questionObj.@R_397_5696@etails[0]}; } }); }); } }
在检查类型定义是否与您最初预期的一样(并正确使用它)之后,您将防止出现大量错误.
以上是大佬教程为你收集整理的Angular 2 – 具有动态对象/属性的ngModel全部内容,希望文章能够帮你解决Angular 2 – 具有动态对象/属性的ngModel所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。