![](/img/trans.png)
[英]How can I use a JavaScript function in a Angular TypeScript component?
[英]How can i call angular/typescript function from AIML? if not possible, how can i make Javascript function call angular/typescript function?
我正在嘗試使AIML模板從file.component.ts中調用SubmitButton()函數,但是AIML是基於XML的標記語言,因此它不會接受(click)之類的語法。 因此,我嘗試先使用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
}
file.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”將導致函數未定義。 我也嘗試了其他方法,但似乎不起作用。 我可以知道什么是正確的方法嗎? 我以前從未做過這樣的事情,也找不到任何可行的方法。
一種選擇是使用有角單擊來調用您的組件方法。 然后,此組件方法將調用您的javascript方法。
<button type="button" (click)="componentFunction()" >Check</button>
componentFunction(){
triggerFunctionInJSFile();
}
注意:您的js函數應該在全局范圍內。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.