角度意外解析错误令牌

时间:2018-03-22 19:12:29

标签: html angular parsing

我在Angular模板中遇到了解析错误。它似乎是由声明的“var”引起的,但我不确定是否允许声明“var”或者是否是更好的方法。

这是解析错误:

Uncaught Error: Template parse errors:
Parser Error: Unexpected token var at column 1 in [var index = 
customers.indexOf(customer);
        var strtIndex = index - 1; customers.splice(strtIndex, index)] in 
ng:///AppModule/AccountCreationDialogComponent.html@20:51 ("tem" 
[(ngModel)]="customer.id"></mat-list-item>
        <button mat-icon-button color="accent" [ERROR ->](click)="var index 
= customers.indexOf(customer);
        var strtIndex = index - 1; customers.s"): 
ng:///AppModule/AccountCreationDialogComponent.html@20:51
Parser Error: Unexpected token var at column 54 in [var index = 
customers.indexOf(customer);

这是我的代码:

 <mat-dialog-content>
    <mat-form-field>
        <input matInput type="text" placeholder="Factor" required [(ngModel)]="account.factor"/>
    </mat-form-field>
    <mat-form-field>
        <input matInput type="text" placeholder="Active" [(ngModel)]="account.active"/>
    </mat-form-field>
    <mat-form-field>
        <button class="menuButton" mat-button [matMenuTriggerFor]="typesOptions">Options</button>
        <mat-menu #typesOptions="matMenu">
            <button mat-menu-item (click)="chooseBasic();">BASIC</button>
            <button mat-menu-item (click)="chooseSilver();">SILVER</button>
            <button mat-menu-item (click)="chooseGold();">GOLD</button>
        </mat-menu>
        <input matInput type="text" placeholder="Type" [(ngModel)]="account.type"/>
    </mat-form-field>
    <mat-form-field>
        <input matInput type="text" placeholder="Customers" [(ngModel)]="account.customerList"/>
        <mat-list *ngFor="let customer of customers">
            <mat-list-item role="listitem" [(ngModel)]="customer.id"></mat-list-item>
            <button mat-icon-button color="accent" (click)="var index = customers.indexOf(customer);
            var strtIndex = index - 1; customers.splice(strtIndex, index)">
                <mat-icon aria-label="Delete">delete</mat-icon>
            </button>
        </mat-list>
    </mat-form-field>
    <mat-header-cell *matHeaderCellDef>
        <button mat-icon-button color="primary" (click)="addCustomer()">
            <mat-icon aria-label="Example icon-button with a heart icon">add</mat-icon>
            Add Customer
        </button>
    </mat-header-cell>
</mat-dialog-content>
<mat-dialog-actions>
    <button mat-raised-button mat-dialog-close>Cancel</button>
    <button mat-raised-button (click)="create()">Create Account</button>
    </mat-dialog-actions>

1 个答案:

答案 0 :(得分:0)

您不能在HTML模板中使用'var'。您必须使用'let'来实例化变量:

(click)="let index = customers.indexOf(customer); let strtIndex = index - 1; customers.splice(strtIndex, index)">