【问题标题】:How can i call angular/typescript function from AIML? if not possible, how can i make Javascript function call angular/typescript function?如何从 AIML 调用 angular/typescript 函数?如果不可能,我怎样才能让 Javascript 函数调用 angular/typescript 函数?
【发布时间】:2019-08-16 08:09:10
【问题描述】:

我正在尝试使 AIML 模板 调用 file.component.ts 中的 submitButton() 函数,但是 AIML 是一种基于 XML 的标记语言,因此它不会接受诸如 (click) 之类的角度语法。正因为如此,我尝试使用 onclick 首先调用 javascript 函数,然后尝试从那里调用 submitButton()。但是我不知道该怎么做。

我还对后端进行了一些更改,以尝试通过将一些 response.output 替换为 Angular 语法来直接调用 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 (request.input)

  <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 angular typescript aiml


    【解决方案1】:

    一种选择是使用调用组件方法的角度单击。然后这个组件方法调用你的javascript方法。

    <button type="button" (click)="componentFunction()" >Check</button>
    
    
    componentFunction(){
      triggerFunctionInJSFile();
    }
    

    注意:你的 js 函数应该在全局范围内。

    【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 2020-09-16
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多