如何在Angular2中调用内联javascript函数?
我想在Angular2或Angular4中调用内联Javascript函数 下面是我的示例代码 *.html如何在Angular2中调用内联javascript函数?,angular,angular2-routing,angular2-template,Angular,Angular2 Routing,Angular2 Template,我想在Angular2或Angular4中调用内联Javascript函数 下面是我的示例代码 *.html <div id="demo" onload="myfunction()"></div> <script> function myfunction() { console.log("working"); var map = new AMap.Map("map", { resizeEnable: true, cente
<div id="demo" onload="myfunction()"></div>
<script>
function myfunction() {
console.log("working");
var map = new AMap.Map("map",
{ resizeEnable: true,
center: [latitude, longitude],
zoom: 8
});
}
</script>
但由于无法导入“AMap”而出现错误
注意:由于第三方库的原因,我应该尝试这样的东西(内联javascript)。在index.html中,放在标记行的下面
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
如果您在添加jquery库时遇到任何问题,也有其他方法可以使用它
几乎所有HTML语法都是有效的模板语法。
元素
这是一个显著的例外;这是禁止的,消除了风险
脚本注入攻击。实际上,
被忽略,而
警告显示在浏览器控制台中
要导入javascript文件,可以将它们包含在项目的
angular cli.json
文件的scripts
数组中。包括jQuery、AMap js文件和用于调用/初始化AMap的自定义脚本。为什么要这样做?什么样的第三方库要求你这样做?你能对库做一些说明吗?请看更新的问题如果你不导入Angular,内联工作是否正常?@CapeAndCowl:如果我添加component.html,它就不工作了。但只有在索引中添加相同的内容,它才能工作。实际上,问题在于AMap。我无法导入它。现在我按照你的建议做了。正在获取“找不到名称'AMap'。如果我导入成功,那么我就可以不使用jQuery.Hmm直接进行导入。。如果你能举出例子,那就太好了。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<div id="demo">
<!-----Code-------->
</div>
ngOnInit(){
$(document).ready(function () {
$('#demo').load(() => {
// Your function Body
console.log("working");
var map = new AMap.Map("map",
{ resizeEnable: true,
center: [latitude, longitude],
zoom: 8
});
})
}
}