Add anchored name components dynamically inside a ngFor

时间:2017-12-18 08:22:33

标签: angular datepicker

I'm developing an Angular 2+ application and I'm trying to generate dynamically as much Material Datepicker as I need (one for each input I will generate in a FormArray).

<style>
img {
    width: 100%;
}
.timetable {
    position: relative;
}
.content {
    position: absolute;
    z-index: 999999;
    bottom: 0;
    color: white;
    padding-left: 21px;
}
</style>



<!DOCTYPE html>
<html>
<head>
    <title>Gallery</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
</head>
<body>

<div class="timetable">
    <div class="row">
        <div class="col-md-4">
            <img src="http://ihearthsv.com/wp-content/uploads/2016/05/Courtesy-of-the-Land-Trust.jpg" alt="set up time" width="30%">
            <div class="content">
                <h3>Hello</h3> 
            </div>
            </div>
            <div class="col-md-4">
            <img src="http://ihearthsv.com/wp-content/uploads/2016/05/Courtesy-of-the-Land-Trust.jpg" alt="doors open" width="30%">
            <div class="content">
                <h3>Hello</h3>
            </div>
        </div>
        <div class="col-md-4">
            <img src="http://ihearthsv.com/wp-content/uploads/2016/05/Courtesy-of-the-Land-Trust.jpg" alt="Foodtrucks" width="30%">
            <div class="content">
                <h3>Hello</h3>
            </div>
        </div>
        </div>
        </div>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>

I have tried to use:

<form [formGroup]="varDataForm" (ngSubmit)="onSubmit()" novalidate>
    <div formArrayName="aggLevels"
            *ngFor="let agLevel of varDataForm.get('aggLevels')?.controls; let aggLevelRow = index;">
        <div [formGroupName]="aggLevelRow">
            <label>{{agLevel?.get('aggregationLevelName')?.value}}</label>
            <div formArrayName="variableDataForLevel"
                    *ngFor="let vardata of agLevel.get('variableDataForLevel')?.controls; let rowIndex = index;">
                <div [formGroupName]="rowIndex">
                    <select formControlName="variableDataId">
                        <option *ngFor="let gs1 of gs1AIs" [value]="gs1.id">{{gs1.description}}</option>
                    </select>
                    <input formControlName="value" placeholder="Valor" [matDatepicker]="myDatepicker-{{rowIndex}}">
                    <mat-datepicker #myDatepicker-{{rowIndex}}></mat-datepicker>
                    <div class="error" *ngIf="vardata.get('value').hasError('required') && vardata.get('value').touched">
                        Obligatorio
                    </div>
                    <button type="button" class="btn" (click)="deleteRow(aggLevelRow, rowIndex)" [disabled]="disableDelete(aggLevelRow, rowIndex)">Borrar</button>
                </div>
            </div>
            <button type="button" class="btn" (click)="addRow(aggLevelRow)" [disabled]="disableAdd()">A&ntilde;adir</button>
        </div>
    </div>
    <button *ngIf="varDataForm.get('aggLevels')?.controls?.length > 0" type="submit" class="btn btn-success" [disabled]="disableSubmit()">Guardar cambios</button>
</form>

But I get an error:

<input formControlName="value" placeholder="Valor" [matDatepicker]="myDatepicker-{{rowIndex}}">
<mat-datepicker #myDatepicker-{{rowIndex}}></mat-datepicker>

But I can't use one for more than one inputs.

Do you know a way to generate NodeInvocationException: Template parse errors: Parser Error: Got interpolation ({{}}) where expression was expected at column 13 in [myDatepicker-{{rowIndex}}] in ng:///AppModuleShared/VarDataComponent.html@24:59 ("scription}}</option> </select> <input formControlName="value" placeholder="Valor" [ERROR ->][matDatepicker]="myDatepicker-{{rowIndex}}"> <mat-datepicker #myDatepicker-{{rowIndex}}></ma"): ng:///AppModuleShared/VarDataComponent.html@24:59 for each <mat-datepicker #myDatepicker></mat-datepicker>?

1 个答案:

答案 0 :(得分:0)

期望代码中没有其他错误,您不需要创建动态模板引用变量,它应该在迭代中正常工作,所以请尝试:

<input formControlName="value" [matDatepicker]="myDatepicker">
<mat-datepicker #myDatepicker></mat-datepicker>

<强> StackBlitz