我正在尝试使用简单的JavaScript函数来操作div。但它使用的变量出错:
originalText未定义
home.ts 代码:
declare var orginalText: any;
imports ...
export class HomePage{
constructor(){...}
ngOnInit () {
$('#changeText').click(function(){
orginalText = $('#content').html();
$('#content').text("This is text changed using jquery");
});
$('#changeText2').click(function(){
$('#content').html(orginalText);
});
home.html 代码:
<a href="#" id="changeText">Click here to change text of div</a>
<a href="#" id="changeText2">Original Div content</a>
<div id="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</div>
当我点击链接时,它表示未定义originalText。
答案 0 :(得分:1)
只需将正常功能更改为 arrow function ,然后使用function hai(data: any, host: any, options: any) {
return {
hello: function () {
return "happy scriptring";
}
};
}
export = hai;
访问orginalText
,
this.orginalText