2016-10-19 17 views
11

mam ten angular2 szablonu:angular2 Nie można mieć wiele powiązań szablonów na jednym elemencie

<template *ngIf="alerts.length > 0"> 
<alert *ngFor="let alert of alerts;let i = index" [type]="alert.type" dismissible="true" (close)="closeAlert(i)"> 
    {{ alert?.msg }} 
</alert> 
    </template> 

otrzymuję te błędy:

zone.js:461 Unhandled Promise rejection: Template parse errors: 
Can't have multiple template bindings on one element. Use only one attribute named 'template' or prefixed with * (" </div> 
    <div *ngSwitchCase="false" class="container p-t-10"> 
    <alert *ngIf="alerts.length > 0" [ERROR ->]*ngFor="let alert of alerts;let i = index" [type]="alert.type" dismissible="true" (close)="closeAlert"): [email protected]:37 

czym problem kładę * ngIf i * ngFor w różne elementy html. Powinno działać. Nie?

oraz:

Can't bind to 'type' since it isn't a known property of 'alert'. (""container p-t-10"> 
    <alert *ngIf="alerts.length > 0" *ngFor="let alert of alerts;let i = index" [ERROR ->][type]="alert.type" dismissible="true" (close)="closeAlert(i)"> 
     {{ alert?.msg }} 
    </alert> 
"): [email protected]:80 

dodałem

*ngIf="alerts.length > 0 aby uniknąć przypadków alert = []. Jak mogę to naprawić inaczej?

+0

spróbuj 'attr.type' zamiast typu prostego –

Odpowiedz

13

Numer * w *ngFor powoduje, że Angular ma dodawać znacznik <template>. Na tagu <template> nie ma to sensu i dlatego tutaj dyrektywy strukturalne mają inną składnię.

<template ngFor [ngForOf]="alerts" let-alert let-i="index"> 

Bo inna składnia dla prawie taka sama w różnych miejscach spowodował sporo zamieszania kątowej zespół niedawno wprowadzony

<ng-container> 

że zachowuje się podobnie do znacznika <template> (nie zostanie dodany do DOM), ale pozwala na bardziej wspólnej składni

<ng-container *ngIf="alerts.length > 0"> 
    <alert *ngFor="let alert of alerts;let i = index" [type]="alert.type" dismissible="true" (close)="closeAlert(i)"> 
    {{ alert?.msg }} 
    </alert> 
</ng-container> 
+0

ten pusty Sprawdź tablica rozwiąże również' nie może wiązać się z " type ", ponieważ nie jest znaną właściwością" alertu "? –

+0

nadal pojawia się ten błąd: "Nie można powiązać z" typem ", ponieważ nie jest on znaną właściwością" alertu ". (" ] [typ] =" alert.type " = "true" (close) = "closeAlert (i)"> {{alert? .msg}} "): b @ 6: 54' –

+0

Prawdopodobnie brakuje importu lub deklaracji w' @NgModule ({import: [ModuleThatContainsAlert], deklaracja: [AlertComponent]) 'zależnie od skąd pochodzi' '. Komunikat o błędzie oznacza, że ​​dla tego elementu nie ma instancji 'AlertComponent'. –

Powiązane problemy