Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

Angular - NgModel in ngFor not update var

I try to create a dynamic setting option in app, but the ngmodel seems does'nt work correctly..

That my code in componnent.ts :

categories = [
    {title: 'General', options: [
        {settingsService: this.settingsService.option.one, description: '...'},
        {settingsService: this.settingsService.option.two, description: '...'},
    ]},
    {title: 'Group', options: [
        {settingsService: this.settingsService.option.three, description: '...'},
        {settingsService: this.settingsService.option.three, description: '...'},
    ]},];

constructor(
    public applicationService: ApplicationService
) { }

Here this.settingsService.option.* is a boolean

And my code in component.html

    <div class="col-xs-12" *ngFor="let category of categories">
        <h3>{{category.title}}</h3>

        <div class="col-xs-12" *ngFor="let option of category.options">

            <mat-checkbox color="primary" [(ngModel)]="option.settingsService">{{option.description | translate }}</mat-checkbox>

        </div>
    </div>

I search on google and i found reactive forms, but with the structure of my var "categories" and the "title" i don't know how to adapt... That what i hope you have a solution to use ngmodel.

Maybe use [(ngModel)]="this[option.settingsServices]", but in a double loop I don't find how it's work...

The problem i have is when i click on the checkBox of the first "this.settingsService.option.three" the second one doesn't update but he referer to the same var...

question from:https://stackoverflow.com/questions/65883808/angular-ngmodel-in-ngfor-not-update-var

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...