简体   繁体   English

Angular 11,如何对 function 中的值进行数据绑定

[英]Angular 11, how to data-bind a value from a function

I got a list of values that has to be shown, i'm using two elements for databing, here's my component:我有一个必须显示的值列表,我使用两个元素进行数据记录,这是我的组件:

export class HomeComponent implements OnInit {
  element!: HomeData;
  element2!: HomeData2;
  /*elements = elements;*/
  constructor(private homeService: HomeService,
              public dialog: MatDialog) {
   }

  ngOnInit(): void {
    this.homeService.getData().subscribe((data: HomeData) => {this.element = data; });
    this.homeService.getData2().subscribe((data2: HomeData2) => {this.element2 = data2; });
  }

and here's my html document这是我的 html 文档

<ul>
  <li>Total number of Sequences: <b>{{element.sequences}}</b></li>
    <li><a href="#"></a>Distinct Prefixes involved in Sequences: <b>{{element.prefixes}}</b></li>
    <li><a href="#"></a>BGP Updates involved in Sequences: <b></b> (Announces: <b>{{element.announces}}</b>, Withdrawals: <b>{{element.withdraws}}</b>)</li>
    <li><a href="#"></a>Total number of BGP Updates collected by RRC00 in 2019: <b>{{element2.updates}}</b> (Announces: <b>{{element2.announces}}</b>, Withdraws: <b>{{element2.withdraws}}</b>)</li>
    <li><a href="#"></a>Percentage of BGP Updates belonging to Sequences: /// (Announces: ///, Withdrawals: ///)</li>
    <li><a href="#"></a>Distinct Collector Peers (CPs) that observed at least a Sequences: <b>{{element.cPs}}</b></li>
    <li><a href="#"></a>Distinct ASes originating the prefixes involved in the Sequences: <b>{{element.aSes}}</b></li>
    <li><a href="#"></a>Number of AB-BA Sequences (whose AS-path contains pattern xAyBz and x'By'Az'): <b>{{element.containingLoops}}</b></li>
    <li><a href="#"></a>Sequences that are originated by an IPv4 Prefix: <b>{{element.prefixv4}}</b></li>
    <li><a href="#"></a>Sequences that are originated by an IPv6 Prefix: <b>{{element.prefixv6}}</b></li>
    <li><a href="#"></a>Sequences whose prefix is announced by more than one AS: <b>{{element.moreThanOneAsOrigin}}</b></li>
    <li><a href="#"></a>Sequences that contain at least one announcement with the BGP Path Attribute Aggregator set: <b>{{element.containsAggregator}}</b></li>
    <li><a href="#"></a>Sequences that contain at least two announcement with different values of the BGP Path Attribute Aggregator: <b>{{element.aggregatorChanges}}</b></li>
    <li><a href="#"></a>Sequences originated by a known beacon prefix: <b>{{element.beaconSequences}}</b></li>
    <li><a href="#"></a>BGP Updates originated by a known beacon prefix: <b>{{element.beaconAnnouncements + element.beaconWithdrawals}}</b> (Announcements: {{element.beaconAnnouncements}}, Withdrawals: {{element.beaconWithdrawals}})</li>
    <li><a href="#"></a>Percentage of BGP Updates originated by a known beacon prefix: /// (Announcements: ///, Withdrawals: ///)</li>
</ul>

Everything works fine but one of the values I have to show is the sum of {{element.announces}} and {{element.withdraws}}, I have tried adding a 'sum' function in my component and then calling it in my html document like this:一切正常,但我必须显示的值之一是 {{element.announces}} 和 {{element.withdraws}} 的总和,我尝试在我的组件中添加一个“总和”function,然后在我的html 文件如下:

<li><a href="#"></a>BGP Updates involved in Sequences: <b>'sum({{element.announces}}, {{element.withdraws}})'</b> (Announces: <b>{{element.announces}}</b>, Withdrawals: <b>{{element.withdraws}}</b>)</li>

The output is exactly the string written there, not the value returned from the function. output 正是写在那里的字符串,而不是从 function 返回的值。 What is the correct syntax for what I'm trying to do, is a function even necessary?我正在尝试做的正确语法是什么,function 甚至是必要的吗?

  1. Binding functions to properties and calling them from interpolation would trigger the function for each change detection cycle in case of default change detection strategy.在默认更改检测策略的情况下,将函数绑定到属性并从插值中调用它们将为每个更改检测周期触发 function。 It might have a performance impact if the function has high overhead.如果 function 的开销较高,则可能会对性能产生影响。
  2. The statements inside the interpolation operator are valid TS expressions.插值运算符中的语句是有效的 TS 表达式。 As such you could do a simple summation using + inside the interplation.因此,您可以在插值中使用+进行简单的求和。 You could also prefix the variables with a + to cast them to number.您还可以在变量前面加上+以将它们转换为数字。

Try the following尝试以下

<li>
  <a href="#"></a>
  BGP Updates involved in Sequences: <b>{{ +element.announces + +element.withdraws }}</b> 
  (Announces: <b>{{ element.announces }}</b>, Withdrawals: <b>{{ element.withdraws }}</b>)
</li>

You need to put your method call inside the {{ }} .您需要将方法调用放在{{ }}中。

Eg例如

{{ sum(element.announces, element.withdraws) }} 

Try to bind the sum method this way {{sum(element.announces,element.withdraws)}}尝试以这种方式绑定 sum 方法{{sum(element.announces,element.withdraws)}}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM