簡體   English   中英

Angular2:無法綁定到“ ngPluralCase”,因為它不是“ x”的已知屬性

[英]Angular2 : Can't bind to 'ngPluralCase' since it isn't a known property of 'x'

首先,這個問題不是重復的,大多數諸如“無法綁定到'x'...”之類的問題的答案都是關於未導入的模塊的,我已經導入了正確的模塊。

我正在關注有關ngPluralngPluralCase指令的angular.io文檔:

 <some-element [ngPlural]="value"> <ng-container *ngPluralCase="'=0'">...</ng-container> <ng-container *ngPluralCase="'other'">...</ng-container> </some-element> 

(......)

從@ angular / common / index導出,在@ angular / common / src / directives / ng_plural.ts中定義

當我嘗試運行此代碼時,出現錯誤(請在plnkr中查看)

無法綁定到“ ngPluralCase”,因為它不是“ ng-container”的已知屬性。

main.ts:

import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { Component, NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";

@Component({
  selector: "my-app",
  template:`
  <div [ngPlural]="1">
      <ng-container *ngPluralCase="'=0'">...</ng-container>
      <ng-container *ngPluralCase="'other'">...</ng-container>
  </div>
  `,
  styles: []
})
class AppComponent {

}

@NgModule({
  imports: [
    BrowserModule
  ],
  declarations: [AppComponent],
  exports: [AppComponent],
  bootstrap: [AppComponent]
})
class AppModule { }


platformBrowserDynamic().bootstrapModule(AppModule);

我進口BrowserModule (其中出口CommonModule ,但我也試過進口CommonModule直接)在我AppModule ,正如你所看到的,誤差約為ngPluralCase而不是ngPlural這是同一個模塊和沒有問題使用...

所以我的問題是:

有人知道這里發生了什么嗎? 是與這些指令的“實驗”狀態有關的錯誤,還是我缺少某些東西?

PS :我正在使用angular v2.1.0

目前,我在Github存儲庫上打開了一個問題 ,因為它似乎真的壞了。

編輯這是從Github問題獲得答案

這里的問題實際上是NgPluralCase指令使用@Attribute注入,並且由於ng-container是虛擬節點,因此無法在其上檢索屬性。

在這種情況下,您必須使用<template>標簽,這些文檔將作為待定PR的一部分進行修復。

答案是angular.io現在已經過時,至少是NgPlural的示例。
以下示例適用於v2.4.4:

<some-element [ngPlural]="value">
  <template ngPluralCase="=0">there is nothing</template>
  <template ngPluralCase="=1">there is one</template>
  <template ngPluralCase="few">there are a few</template>
</some-element>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM