Javascript——为什么我在TypeScript文件中编写的Javascript不能正常工作?
我创建了一个爱奥尼亚项目。我在ionic项目的类型脚本中添加了JavaScript代码,但我发现该函数不起作用Javascript——为什么我在TypeScript文件中编写的Javascript不能正常工作?,javascript,html,typescript,Javascript,Html,Typescript,我创建了一个爱奥尼亚项目。我在ionic项目的类型脚本中添加了JavaScript代码,但我发现该函数不起作用 problem.html </ion-col> <ion-col col-7 style="text-align:center;"> <div class="center-block"> <!--<input type="file" id="view" accept="image/*" multiple="mu
problem.html
</ion-col>
<ion-col col-7 style="text-align:center;">
<div class="center-block">
<!--<input type="file" id="view" accept="image/*" multiple="multiple">-->
<!--<input type="file" accept="image/*" multiple="multiple" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">-->
<article>
<input id="files" type="file" accept="image/*" multiple/>
<button ion-button (click)="imageUpload()"> Here ! </button>
</article>
</div>
</ion-col>
<p style="text-align:center;">
<output id="result">
<div>
</div>
</output>
在这里
在my problem.ts文件中:
imageUpload(){
window.onload = function () {
console.log("hihi");
//Check File API support zzzz
(<any>$('#files')).live("change", function (event) {
var files = event.target.files; //FileList object
var output = document.getElementById("result");
for (var i = 0; i < files.length; i++) {
var file = files[i];
//Only pics
// if(!file.type.match('image'))
if (file.type.match('image.*')) {
if (this.files[0].size < 2097152) {
// continue;
var picReader = new FileReader();
picReader.addEventListener("load", function (event) {
var picFile = <FileReader>event.target;
var div = document.createElement("div");
div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
"title='preview image'>";
output.insertBefore(div, null);
});
//Read the image
$('#clear, #result').show();
picReader.readAsDataURL(file);
} else {
alert("Image Size is too big. Minimum size is 2MB.");
$(this).val("");
}
} else {
alert("You can only upload image file.");
$(this).val("");
}
}
});
(<any>$('#files')).live("click", function () {
$('.thumbnail').parent().remove();
$('result').hide();
$(this).val("");
});
(<any>$('#clear')).live("click", function () {
$('.thumbnail').parent().remove();
$('#result').hide();
$('#files').val("");
$(this).hide();
});
}
}
imageUpload(){
window.onload=函数(){
控制台日志(“hihi”);
//检查文件API支持zzzz
($('#文件')).live(“更改”,函数(事件){
var files=event.target.files;//文件列表对象
var输出=document.getElementById(“结果”);
对于(var i=0;i
这是我所有的代码。在我实现它之后,没有返回任何错误,但是我发现我的JavaScript,特别是imageUpload()
函数,没有工作。Iconsole.log
并且在我的控制台中不返回任何内容
有人知道问题出在哪里吗?谢谢。显然要检查的是,调用此方法时是否有有效的窗口对象?是否调用过window.onLoad()?我不记得在ionic应用程序中使用过window.onLoad()
在哪里调用此方法imageUpload()?我可能会首先获得一个有效的窗口对象(例如,在组件或服务中),然后将其传递到这个方法中。而且,我建议更改代码,以便在Angular/Ionic组件生命周期方法中调用“onload”函数。从代码的外观来看,我怀疑它是否会在Angular/Ionic应用程序中运行。Ya..我已经检查并控制台了.log(),没有调用windown.onload()。我是Ionic的新手,他们告诉我,在Java中工作的一切都将在typescript中工作。但是我不知道为什么window.onload不工作。你知道用什么替换window.onload吗???提前谢谢。当我点击按钮时,imageUpload()函数被调用。如何编辑它,使其在我的应用程序中工作??