Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有Ngif或click函数的情况下调用angular中的Ts函数?_Angular - Fatal编程技术网

如何在没有Ngif或click函数的情况下调用angular中的Ts函数?

如何在没有Ngif或click函数的情况下调用angular中的Ts函数?,angular,Angular,我需要在没有ngif的情况下调用ts函数,或者在html中使用angular调用click函数。 我需要检查函数并在html中绑定这些值。由于它有3个条件,我无法在html中使用通用ngif。请给我建议 <td> checkEmployee(Employee) {{employeeresult}} </td> checkEmployee(Employee){ if (employee.result=='Excellent'){ this.emplo

我需要在没有ngif的情况下调用ts函数,或者在html中使用angular调用click函数。 我需要检查函数并在html中绑定这些值。由于它有3个条件,我无法在html中使用通用ngif。请给我建议

<td>
checkEmployee(Employee)
{{employeeresult}}

</td>




 checkEmployee(Employee){
  if (employee.result=='Excellent'){
    this.employeeresult='Approve';
    
  }
  else if (employee.result=='Good'){
    this.employeeresult='Withheld';
  }
  else{
    this.employeeresult='Reject';
  }

检查员工(员工)
{{employeeresult}
检查员工(员工){
如果(employee.result=='Excellent'){
这个。employeeresult='Approve';
}
else if(employee.result=='Good'){
这个.employeeresult='depresented';
}
否则{
此。employeeresult='Reject';
}

您可以返回它并在模板中绑定函数,如下所示:

<td>
{{checkEmployee(employee)}}
</td>




 checkEmployee(employee){
  if (employee.result=='Excellent'){
    return 'Approve';
  } else if (employee.result=='Good'){
    return 'Withheld';
  } else {
    return 'Reject';
  }

{{checkEmployee(employee)}
检查员工(员工){
如果(employee.result=='Excellent'){
返回“批准”;
}else if(employee.result=='Good'){
返回“扣留”;
}否则{
返回“拒绝”;
}

另一种方法是使用管道。与函数调用相比,使用管道的性能更高。

Hello@viswanath您需要使用
return
从条件中获取标签

比如说

HTML

<div *ngFor="let employee of employeess">
    {{checkEmployee(employee)}}
</div>

您还可以使用代码

我的答案与其他实现不同,因为我建议您使用管道,而不是调用模板中的方法

<td>
{{ employee | employeePipe }}
</td>

只需使用退货statement@NidhinKumar我的问题是html。它不能识别它是一种方法。即使在Div标签中,如果我们在ngif语句中只提供了它,它也会将其视为一种方法Hello@viswanath查看我下面的帖子,这样你就会知道我的答案是否比voteup有用,并且接受了最佳答案,这样其他人就可以轻松找到;)@WillaJuan谢谢。正如William Juan提到的,我建议使用管道,而不是出于性能原因在模板中调用函数!
<td>
{{ employee | employeePipe }}
</td>
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'employeePipe'
})
export class employeePipe implements PipeTransform {

  transform(employee: EmployeeModel): string {
    switch (employee.result) {
      case 'Excellent' : return 'Fonctionnel';
      case  'Good': return 'Technique';
      default:
              return 'Reject'
        break;
    }
    return null;
  }

}