javascript - 从 typescript 调用使用 c# 注册的 javascript 函数
问题描述
我使用 RegisterStartupScript 从 C# 添加了一个函数调用,如下所示:
Page.ClientScript.RegisterStartupScript(this.GetType(), "PrintWindowJScript", "<script type=\"text/javascript\">OpenNewWindow() { Reports_OpenWindowNamed(URL, 350, 250, \"PrintAttachPopupWindow\"); }</script>");
上面的代码会将它添加到 DOM 中,所以当我调用 OpenNewWindow() 时,它应该从我的 Angular 组件中调用该函数。
现在在我的角度组件中,我在 Typescript (Angular) 中有一个下拉更改功能。我需要从这里调用 OpenNewWindow(),如下所述。
interface IMyWindow {
OpenNewWindow: Function;
}
declare var v_MyWindow: IMyWindow ;
export class MyComponent implements OnInit {
ngOnInit() {
}
attachChange(attachDropDown: any) {
if(attachDropDown.value && attachDropDown.value.id == "MyValue"){
v_MyWindow.OpenNewWindow();
}
}
}
上面的代码给了我未定义的“v_MyWindow”。
我试过参考这篇文章,但没有用。
如果我遗漏了什么,谁能告诉我?我应该怎么做才能调用该函数?
解决方案
您已经创建了接口并声明了变量v_MyWindow
,您只需要分配属性OpenNewWindow
。
改变:
Page.ClientScript.RegisterStartupScript(this.GetType(), "PrintWindowJScript", "<script type=\"text/javascript\">OpenNewWindow() { Reports_OpenWindowNamed(URL, 350, 250, \"PrintAttachPopupWindow\"); }</script>");
到:
Page.ClientScript.RegisterStartupScript(this.GetType(), "PrintWindowJScript", "<script type=\"text/javascript\">v_MyWindow.OpenNewWindow = function() { Reports_OpenWindowNamed(URL, 350, 250, \"PrintAttachPopupWindow\"); }</script>");
推荐阅读
- python - 如何使用 set_random_seed 获得相同的结果
- fmi - 如何使用 PyFMI 运行 EnergyPlus-FMU
- python - QPixmap(图像)的图块具有重叠区域
- python - 在 Windows 10 中为 python 3 创建虚拟环境时遇到问题
- java - 如何将参数发送到 AES 加密器?
- arrays - Swift 5根据对象值分组和拆分对象数组
- python - 如何使用 Mask R-CNN 和背景减法来掩盖敞篷卡车的负载?
- python - 如何将 PyCharm 文件中的数据导出到 Excel 工作表中?
- javascript - 防止iframe js自动滚动父页面
- html - 我尝试将 HTML 网站转换为 WordPress,但只显示内容并且 CSS 不起作用