在Angular Typescript中显示Function的返回值

时间:2018-06-15 11:32:47

标签: javascript angular typescript

我正在使用angular,我想在HTML中显示函数的结果。当我使用



#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>

#define MAX_LINE_LENGTH 1024

void  print_lines(FILE *stream) {
    double a, b, c;
    char line[MAX_LINE_LENGTH];
    while (fgets(line, MAX_LINE_LENGTH, stream) != NULL) {
        char *pnt;
        // locate substring `<start>` in the line
        if ((pnt = strstr(line, "<start>") != NULL)) {
            // advance pnt to point to the characters after the `<start>`
            pnt = &pnt[sizeof("<start>") - 1];
            char *pntend;

            // scan first number
            a = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }
            pnt = pntend;
            // scan second number
            b = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }
            pnt = pntend;
            // scan third number
            c = strtod(pnt, &pntend);
            if (pnt == pntend) {
                fprintf(stderr, "Error converting a value.\n");
                // well, handle error some better way than ignoring.
            }

            printf("Read values are %lf %lf %lf\n", a, b, c);
        } else {
            // normal line
            //fputs(line, stdout);
        }
    }
}

int main()
{
    print_lines(stdin);
    return 0;
}
&#13;
&#13;
&#13;

HTML中的

然后是{{latlng}},它总是显示整个函数的代码,而不是返回值,即使我只是返回latlng值。我哪里错了?

查看问题: assign function return value to some variable using javascript

整个.ts文件:

&#13;
&#13;
  var geo = function onLocationFound(e) {
  var radius = e.accuracy / 2;
  let marker = new L.Marker(e.latlng, {
    draggable: true,
    icon: Icon
  }).bindPopup(""+e.latlng);;
  map.addLayer(marker);
  return e.latlng
  }      
  this.latlng = geo;
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您将函数本身分配给this.latlng,而不是调用函数并分配结果。通过添加括号来调用该函数:

this.latlng = geo();

答案 1 :(得分:0)

尝试以下代码:

function onLocationFound(e) {
      var radius = e.accuracy / 2;
      let marker = new L.Marker(e.latlng, {
        draggable: true,
        icon: Icon
      }).bindPopup(""+e.latlng);;
      map.addLayer(marker);

      this.latlng = e.latlng
      }
    }

在HTML中:

{{ latlng }}

如果你想把它传递到其他地方或其他函数,那么使用this.latlng作为函数参数