0

I have this code:

  <ng-container matColumnDef="pricingTemplateId">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Margin</th>
            <td mat-cell *matCellDef="let customer">
                {{customer.pricingTemplateName}}
            </td>
        </ng-container>

        <ng-container matColumnDef="PricingTemplatesList">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Margin Template</th>
            <td mat-cell *matCellDef="let customer">
                <select id="pricingtemplates" name="ddlPricingTemplate">
                    <option *ngFor="let pricingTemplate of customer.pricingTemplatesList" [(ngModel)]="customer.pricingTemplateName">{{pricingTemplate.name}}</option>
                </select>
            </td>
        </ng-container>

For the select control I'm attaching array of objects like in the screenshot below:

enter image description here

Now, I also have another string value in the model on which I want to base the selected value in the dropdown.

When I do [(ngModel)]="customer.pricingTemplateName" which is basically a string I'm getting this message "No value accessor for form control with unspecified name attribute"

Without the ngModel property I'm able to list all the template names in the dropdown but do I need to do some change in order to set the selected value based on a string?

0

2 Answers 2

1

Use the compareWith check the link https://netbasal.com/understanding-the-comparefn-input-in-angular-v4-4a401ef4fc4c

Sign up to request clarification or add additional context in comments.

Comments

0

You are performing 2 way data binding with [(ngModel)] on the <option> for a start. This would need to go on the <select> And then you can set the [value]="pricingTemplate.name" of option within the ngFor loop and this will get picked up by the select element.

<select id="pricingtemplates" name="ddlPricingTemplate" [(ngModel)]="customer.pricingTemplateName">
   <option *ngFor="let pricingTemplate of customer.pricingTemplatesList" [value]="pricingTemplate.name">
     {{pricingTemplate.name}}
   </option>
</select>

4 Comments

Thanks @Sam but I'm still getting this error "No value accessor for form control with unspecified name attribute"
This is what I did in the option line: <option *ngFor="let pricingTemplate of customer.pricingTemplatesList" [(ngModel)]="customer.pricingTemplateName" value="customer.pricingTemplateName">{{pricingTemplate.name}}</option>
I have provided an example of what i mean.
Thanks, that works, the only thing you were missing were the double brackets which I added for the [value] in the option control. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.