我正在尝试使AIML模板从file.component.ts调用submitButton()函数,但是 AIML 是一种基于 XML 的标记语言,因此它不接受诸如(点击)之类的 Angular 语法。因此,我尝试首先使用 onclick 调用 JavaScript 函数,然后尝试从那里调用submitButton()。但是我不知道该怎么做。
我还对后端做了一些更改,尝试通过用 Angular 语法替换一些 response.output 来直接调用 Angular,希望它能工作。但事实并非如此..
我正在尝试使用 JavaScript 函数从 Angular/TypeScript 调用该函数。我怎样才能做到这一点?
AIML(响应.输出)
<template>Select an option
<button id="image1" onclick="triggerFunctionInJSFile()" type="submit">
<img height="200" ngmodel="chatMessageModel.input" src="https://scene7.zumiez.com/is/image/zumiez/pdp_hero/adidas-Trefoil-White-%26-Black-T-Shirt-_289236-front-US.jpg" width="200"/> Image 1
</button>
<button id="image2" onclick="triggerFunctionInJSFile()" type="submit">
<img height="200" ngmodel="chatMessageModel.input" src="https://scene7.zumiez.com/is/image/zumiez/pdp_hero/adidas-Trefoil-White-%26-Black-T-Shirt-_289236-front-US.jpg" width="200"/> Image 2
</button>
</template>
AIML(请求.输入)
<pattern>img options</pattern>
BackendChanges.java
if(respMsg.getOutput().contains("onclick") || respMsg.getOutput().contains("ngmodel")) {
String click = respMsg.getChatBotMsg().getOutput();
click = click.replaceAll("\\bonclick\\b", "(click)");
String model = click;
model = model.replaceAll("\\bngmodel\\b", "[(ngModel)]");
respMsg.getChatBotMsg().setOutput(model);
}
jsfile.js
function triggerFunctionInJSFile() {
//To Do
console.log("Triggered the function in js file ");
fileComponent = require('./dir/file.component'); // i tried using require but doesn't work
fileComponent.submitButton(); // calling the function from file component
}
文件.component.ts
@Component({
selector: 'app-file',
templateUrl: './file.component.html',
styleUrls: ['./file.component.scss']
})
export class FileComponent implements OnInit{
constructor {}
ngOnInit() {}
public submitButton() {
//submit function here
}
}
在 jsfile.js 中,“require”将导致函数未定义。我也尝试过其他一些方法,但似乎不起作用。我可以知道执行此操作的正确方法是什么吗?我以前从未做过这样的事情,而且找不到任何有效的方法。
最佳答案
一种选择是使用调用组件方法的 Angular 单击。然后这个组件方法调用您的 javascript 方法。
<button type="button" (click)="componentFunction()" >Check</button>
componentFunction(){
triggerFunctionInJSFile();
}
注意:你的 js 函数应该在全局范围内。
关于javascript - 我如何从 AIML 调用 Angular/TypeScript 函数?如果不可能,我怎样才能让Javascript函数调用 Angular/ typescript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521044/