简体   繁体   English

错误:模板解析错误:无法绑定到“评级”,因为它不是“评级”的已知属性

[英]Error: Template parse errors: Can't bind to 'rating' since it isn't a known property of 'rating'

Im trying to make a rating system in a component, to import it in other pages in my app我试图在一个组件中创建一个评级系统,将它导入到我的应用程序的其他页面中

this is my the piece of code in home.html that give me error (Where I want the rating):这是我在 home.html 中给我错误的一段代码(我想要评级的地方):

<rating [(rating)]="rating"> </rating>

this is the code in rating.html:这是 rating.html 中的代码:

<div>
  <ion-icon name="star" *ngFor="let num of[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
  (click)="rate(num)" [ngStyle]="{'color': getColor(num)}">
</ion-icon>
</div>

and this is my code in rating.component.ts:这是我在 rating.component.ts 中的代码:

@Input() rating: number;

@Output() ratingChange: EventEmitter<number> = new EventEmitter();

  constructor() { }

  rate(index: number){
    // function used to change the value of our rating 
    // triggered when user, clicks a star to change the rating
    this.rating = index;

    this.ratingChange.emit(this.rating);

  }

  getColor(index: number) {
    /* function to return the color of a star based on what
     index it is. All stars greater than the index are assigned
     a grey color , while those equal or less than the rating are
     assigned a color depending on the rating. Using the following criteria:

          1-2 stars: red
          3 stars  : yellow
          4-5 stars: green 
    */

    if(this.isAboveRating(index)){
      return COLORS.GREY;
    }

    switch(this.rating){
      case 1:
      case 2:
      case 3:
      case 4:
        return COLORS.RED;
      case 5:
      case 6: 
        return COLORS.YELLOW;
      case 7:
      case 8: 
      case 9:
      case 10:
        return COLORS.GREEN;
      default:
        return COLORS.GREY;
    }

  }

  isAboveRating(index: number): boolean {
    // returns whether or not the selected index is above ,the current rating
    // function is called from the getColor function.

    return index> this.rating;
  }

}

enum COLORS {
  GREY = "#E0E0E0",
  GREEN = "#76FF03",
  YELLOW = "#FFCA28",
  RED = "#DD2C00"
}

I declared the component in the home.module.ts like this:我在 home.module.ts 中声明了这个组件,如下所示:

import { RatingComponent } from '../rating/rating.component';

@NgModule({
  imports: [
    CommonModule,
    FormsModule,
    IonicModule,
    RouterModule.forChild([
      {
        path: '',
        component: HomePage
      }
    ])
  ],
  declarations: [HomePage, RatingComponent ],
  schemas: []
})

this is my home.component.ts:这是我的 home.component.ts:

import { Component, ViewChild } from '@angular/core';
import { FBServiceService } from '../services/fbservice.service';
import { Router } from '@angular/router';
import { IonSlides, IonInfiniteScroll } from '@ionic/angular';
import * as CanvasJS from '../../assets/canvasjs-2.2/canvasjs.min.js';


@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
    @ViewChild('dynamicList') dynamicList;
    @ViewChild('SwipedTabsSlider') SwipedTabsSlider: IonSlides;
    @ViewChild('infiniteScroll') ionInfiniteScroll: IonInfiniteScroll;

    SwipedTabsIndicator: any = null;
    tabs = ["selectTab(0)", "selectTab(1)"];
    public category: any = "0";
    ntabs = 2;

    listado=[];

    listadoPanel=[];

    constructor(
      private FBService: FBServiceService,
      private router: Router
    ){

    }

    ionViewDidEnter(){
      this.FBService.leeNotas()
      .subscribe((querySnapshot) => {
        this.listado=[];
        querySnapshot.forEach(doc => {

          this.listado.push({id: doc.id,... doc.data()});

        });

        this.listadoPanel = this.listado;

        this.SwipedTabsIndicator = document.getElementById("indicator");

        let chart = new CanvasJS.Chart("chartContainer", {
          animationEnabled: true,
          title: {
            text: "Tier List "
          },
          axisX: {
            interval: 1
          },
          axisY: {
            title: "Points",
            scaleBreaks: {
              type: "wavy",
              customBreaks: [{
                startValue: 80,
                endValue: 210
                },
                {
                  startValue: 230,
                  endValue: 600
                }
            ]}
          },
          data: [{
            type: "bar",
            toolTipContent: "&lt;img src=\"https://canvasjs.com/wp-content/uploads/images/gallery/javascript-column-bar-charts/\"{url}\"\" style=\"width:40px; height:20px;\"&gt; &lt;b&gt;{label}&lt;/b&gt;&lt;br&gt;Budget: ${y}bn&lt;br&gt;{gdp}% of GDP",
            dataPoints: [
              { label: "Samus", y: 17.8, gdp: 5.8, url: "israel.png" },
              { label: "Pacman", y: 22.8, gdp: 5.7, url: "uae.png" },
              { label: "Canela", y: 22.8, gdp: 1.3, url: "brazil.png"},
              { label: "Zelda", y: 24.3, gdp: 2.0, url: "australia.png" },
              { label: "Sonic", y: 36.8, gdp: 2.7, url: "skorea.png" },
              { label: "Mario", y: 41.1, gdp: 1.2, url: "germany.png" },
              { label: "Luigi", y: 46.1, gdp: 1.0, url: "japan.png" },
              { label: "Kirby", y: 48.3, gdp: 1.9, url: "uk.png" },
              { label: "Ridley", y: 55.9, gdp: 2.5, url: "india.png" },
              { label: "Cloud", y: 69.2, gdp: 5.3, url: "russia.png" },
              { label: "Ike", y: 215.7, gdp: 1.9, url: "china.png" },
              { label: "Incineroar", y: 611.2, gdp: 3.3, url: "us.png" },
              { label: "Samus", y: 17.8, gdp: 5.8, url: "israel.png" },
              { label: "Pacman", y: 22.8, gdp: 5.7, url: "uae.png" },
              { label: "Canela", y: 22.8, gdp: 1.3, url: "brazil.png"},
              { label: "Zelda", y: 24.3, gdp: 2.0, url: "australia.png" },
              { label: "Sonic", y: 36.8, gdp: 2.7, url: "skorea.png" },
              { label: "Mario", y: 41.1, gdp: 1.2, url: "germany.png" },
              { label: "Luigi", y: 46.1, gdp: 1.0, url: "japan.png" },
              { label: "Kirby", y: 48.3, gdp: 1.9, url: "uk.png" },
              { label: "Ridley", y: 55.9, gdp: 2.5, url: "india.png" },
              { label: "Cloud", y: 69.2, gdp: 5.3, url: "russia.png" },
              { label: "Ike", y: 215.7, gdp: 1.9, url: "china.png" },
              { label: "Incineroar", y: 611.2, gdp: 3.3, url: "us.png" }
            ]
          }]
        });
        chart.render();

      })
    }

    ionViewWillEnter() {
      this.category = "0";
      this.SwipedTabsSlider.length().then(l => {  //no sería necesario aquí, solo en ngOnInit
        this.ntabs = l;
      });
    }

    doRefresh(refresher){
      this.FBService.leeNotas()
      .subscribe(querySnapshot => {
        this.listado = [];

        querySnapshot.forEach((doc) => {
          this.listado.push({id:doc.id,...doc.data()});
        });
        this.listadoPanel = this.listado;

        refresher.target.complete();
      });
    }

    /* El método que permite actualizar el indicado cuando se cambia de slide*/
  updateIndicatorPosition() {
    this.SwipedTabsSlider.getActiveIndex().then(i => {
      if (this.ntabs > i) {  // this condition is to avoid passing to incorrect index
        this.SwipedTabsIndicator.style.webkitTransform = 'translate3d(' + (i * 100) + '%,0,0)';
      }
    });
  }
  /* El método que anima la "rayita" mientras nos estamos deslizando por el slide*/
  animateIndicator(e) {
    //console.log(e.target.swiper.progress);
    if (this.SwipedTabsIndicator)
      this.SwipedTabsIndicator.style.webkitTransform = 'translate3d(' +
        ((e.target.swiper.progress * (this.ntabs - 1)) * 100) + '%,0,0)';
  }



  }

In this case you can't bind rating likie this: [(rating)]="rating"在这种情况下,您不能像这样绑定评级: [(rating)]="rating"

You have to listen Emitter separatly:你必须单独听发射器:

<rating [rating]="rating" (ratingChange)="someFunction($event)"> </rating>

尝试以下将 functionx 更改为父组件中函数的名称。

<rating [rating]="rating" (ratingChange)="functionx($event)"> </rating>

You are inputing the rating to your component so that would be:您正在向组件输入rating ,以便:

<rating [rating]="rating".../>

Now since you want to listen to the ratingChange event.现在因为您想收听ratingChange事件。 You need to do it like this:你需要这样做:

<rating [rating]="rating" (ratingChange)="parentFunc($event)"> </rating>

So whenever the ratingChange is emitted, the parentFunc on the parent component will be triggered.所以,只要ratingChange发出时, parentFunc父组件将被触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:模板解析错误:无法绑定到“ ngForOf”,因为它不是“模板”的已知属性 - Error: Template parse errors: Can't bind to 'ngForOf' since it isn't a known property of 'template' Angular中的错误:模板解析错误:无法绑定到&#39;datetime&#39;,因为它不是&#39;time&#39;的已知属性 - Error in Angular: Template parse errors: Can't bind to 'datetime' since it isn't a known property of 'time' 错误:模板解析错误:无法绑定到“myProperty”,因为它不是“myComponent”的已知属性 - Error: Template parse errors: Can't bind to 'myProperty' since it isn't a known property of 'myComponent' 未捕获的错误:模板解析错误:无法绑定到“ FormGroup”,因为它不是“ form”的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'FormGroup' since it isn't a known property of 'form' 错误:模板解析错误:无法绑定到“选项”,因为它不是“图表”的已知属性 - Error: Template parse errors: Can't bind to 'options' since it isn't a known property of 'chart' 未捕获错误:模板解析错误:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Uncaught Error: Template parse errors: Can't bind to 'ngModel' since it isn't a known property of 'input' 未捕获(承诺):错误:模板解析错误:由于它不是“输入”的已知属性,因此无法绑定到“上载器” - Uncaught (in promise): Error: Template parse errors: Can't bind to 'uploader' since it isn't a known property of 'input' 许多失败:模板解析错误:无法绑定到“routerLink”,因为它不是“a”的已知属性。 Karma 中的错误 - Lots of Failed: Template parse errors: Can't bind to 'routerLink' since it isn't a known property of 'a'. errors in Karma Ngx无限滚动 - 模板解析错误:无法绑定到&#39;infiniteScrollDistance&#39;,因为它不是已知属性 - Ngx infinite scrolling - Template parse errors: Can't bind to 'infiniteScrollDistance' since it isn't a known property 模板解析错误:无法绑定到“ngbTypeahead”,因为它不是“input”的已知属性 - Template parse errors: Can't bind to 'ngbTypeahead' since it isn't a known property of 'input'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM