Javascript Angular 2 - 绑定不能包含赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43117917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:41:08  来源:igfitidea点击:

Angular 2 - Bindings cannot contain assignments

javascriptangulartypescript

提问by Nicolas

I need to give a class to a <tr>if a property of item has the same value as a property from an object in an array.

<tr>如果 item 的属性与数组中对象的属性具有相同的值,我需要为 a 提供一个类。

Here's the codeI currently have:

这是我目前拥有的代码

<tr *ngFor="let item of closingDayGroupsList" [class.inactive]="definitionDetails.Groups.filter(i => i.AbsenceReservationGroupID === item.ID).length > 0">

however now I receive the error:

但是现在我收到错误

Bindings cannot contain assignments

I'm not sure if what I'm doing is bad practice, or if I'm just making syntax errors.

我不确定我在做什么是不好的做法,或者我只是在犯语法错误。

This is the only way I know to achieve what I want, but it's not working

这是我所知道的实现我想要的唯一方法,但它不起作用

回答by Sravan

Its a bad practice to use expressionsin angular bindings

它是一种不好的做法,用expressionsangular bindings

Move the classexpressioninto controller.

class表达式移动到控制器中。

export class AppComponent {
    title = 'Groups';

    getClass(item): void {

      // add filter logic here
      return this.definitionDetails.Groups.filter(i => i.AbsenceReservationGroupID === item.ID).length > 0

    }
}

The tr will be something like,

tr 将是这样的,

<tr *ngFor="let item of closingDayGroupsList" [class.inactive]="getClass(item)">

回答by Shubham Dave

There is a little problem with that. Take as an example, that I want to stop showing the date to the user which is equal to 0000.00.00, so, by taking the inline expression binding I can find it right to:

这有点问题。举个例子,我想停止向用户显示等于 0000.00.00 的日期,所以,通过使用内联表达式绑定,我可以找到它:

 {{(appointment_date === "0000.00.00") ? ' ' : appointment_date}}