在没有点击事件的情况下在HTML中调用TypeScript函数 [英] Call TypeScript function in HTML without a click event

查看:372
本文介绍了在没有点击事件的情况下在HTML中调用TypeScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道单击按钮时如何调用TypeScript,但是如何在没有此类事件的情况下调用函数?当我填充了一个称为chartData的数组时,我想调用一个函数.这就是我所拥有的:

I know how to call a TypeScript when clicking a button, but how do you call a function without that type of event? I would like to call a function when I have an array called chartData populated. This is what I have:

        <div *ngIf="chartData.length">
            chartMethod();
        </div>

但不是调用chartMethod(),它只是在html页上打印'chartMethod()'.

But instead of chartMethod() being called, it just prints 'chartMethod()' on the html page.

我也尝试过:

            <script> 
                chartTest();
            </script>

但该函数仍未调用.

推荐答案

之所以会发生这种情况,是因为此chartTest()被Angular封装并且无法在Angular外部访问. 但是只要有角度,您就可以:

It happens because this chartTest() is incapsulated by Angular and is not accessible outside Angular. But inside angular you can, you just need to:

<div *ngIf="chartData.length">
    {{chartMethod()}}
</div>

但是它将在html中添加一个未定义的内容,所以只需测试

But it will add an undefined in html so just test

<div *ngIf="chartData.length">
    {{chartMethod() ? chartMethod() : ""}}
</div>

但是我不认为它是html可调用性的任何方法,它需要由打字稿中的组件本身来完成.

But I don't think it's html resposibillity call any method, it needs to be done by the component itself in typescript.

这篇关于在没有点击事件的情况下在HTML中调用TypeScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆