In our Angular form (we are using template driven forms) we have a list of input fields which are rendered dynamically, as so, we need to validate then. Following this: Using template driven form with dynamic input list (ngFor) I didn't come any further as it seems to be outdated, as the plunkr sample is not working.
that is our tbody which renders our list:
<tbody>
<tr *ngFor="let item of items; let i=index">
<td><span>{{ item.categoryName }}</span></td>
<td class="text-right">
<input *ngIf="editMode"
type="number"
tabindex="1"
autocomplete="off"
[(ngModel)]="item.value" name="value-{{item.categoryId}}" #value="ngModel"
required
/>
{{value.valid}} <!--As I'm using ngForm I was expecting to receive false or true here-->
<span *ngIf="!editMode">{{ item.value }}</span>
</td>
<td>
<app-status [status]="item.status"></app-status>
</td>
</tr>
</tbody>
This works fine, the list can be seen, but the validation does not work.
if I try: {{value.valid}} I get the following error:
ERROR TypeError: Cannot read property 'valid' of undefined
Means, my instance of ngModel did't work.
What am I missing?
The scope of a reference variable is the entire template. Do not define the same variable name more than once in the same template. The runtime value will be unpredictable.from: angular.io/guide/… - DeborahKvaluevariable, but the scope of that variable is not isolated to the input box, it's able to be referenced by the whole scope of the template. So especially where you're looping through and creating these separately in your template, it's possible that something gets messed up along the way, hence the warning: - SaxyPandaBearrequiredattribute? - DAG