[英]How do I call a function that is defined inside of require() outside of the require()?
I am using ArcGIS API for Javascript 3.21. 我正在使用ArcGIS API for Java 3.21。 I have a function inside of the require().
我在require()内部有一个函数。 I want the function to be called when a button is clicked, but the button is outside the require().
我希望在单击按钮时调用该函数,但是该按钮位于require()之外。
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//js.arcgis.com/3.7/js/esri/css/esri.css">
<style>
html, body, #map {
height: 100%;
width: 100%;
margin: 1;
padding: 1;
}
</style>
<script src="//js.arcgis.com/3.7/"></script>
<script>
var map;
require([
"esri/map",
"esri/geometry/Point",
"esri/symbols/SimpleMarkerSymbol",
"esri/graphic",
"esri/layers/GraphicsLayer",
"dojo/domReady!"
], function(
Map, Point, SimpleMarkerSymbol, Graphic, GraphicsLayer
) {
map = new Map("map", {
basemap: "gray",
center: [10,10],
zoom: 3
});
map.on("load", function() {
var graphicslayer = new GraphicsLayer();
map.addLayer(graphicslayer);
});
function hello(){
alert("hello,world!");
}
});
</script>
</head>
<body>
<button type="submit"class="searchButton"onclick="hello()">Search</button>
<div id="map"></div>
</body>
</html>
I can't call hello() in onclick="hello()" because hello() is inside the require(). 我无法在onclick =“ hello()”中调用hello(),因为hello()位于require()内部。
Your hello function is scoped to the require function. 您好函数的作用域是require函数。 You want to scope it to the global object, which is the window object in your case.
您希望将其范围限定为全局对象,在您的情况下为窗口对象。 So either:
所以:
function hello(){
alert("hello,world!");
}
window.hello = hello;
or directly 或直接
window.hello = function(){
alert("hello,world!");
}
But you could also bind your hello function to the click event of your object directly in javascript; 但是,您也可以直接在javascript中将hello函数绑定到对象的click事件; you don't have to widen the scope of your function.
您不必扩大功能范围。 There is likely methods to do so in the dojo library.
dojo库中可能有这样做的方法。 A direct javascript way could be something like
直接的JavaScript方式可能类似于
var myButton = document.querySelectorAll("button.searchButton")[0];
if (myButton) {
myButton.addEventListener("click", hello);
}
any function inside require({....}), will NOT be visible to onclick='xxx('bbb')' require({....})内的任何函数对onclick ='xxx('bbb')'不可见
You have 2 choice: 您有2种选择:
1) move this function outside require({....}) 1)将此功能移到require({....})之外
2) if the function has to be inside require({....}), then you have to make it as global function by ( as @Nicolas said) 2)如果函数必须在require({....})内部,则必须通过(如@Nicolas所说)将其作为全局函数
window.xxx = function(bbb){
alert(bbb);
}
you can't call a function inside a require to use later, because hello function is inside an anonymous function, which require function runs once, and it's not possible to recover what is inside of require function. 您不能在require内部调用某个函数以供以后使用,因为hello函数位于匿名函数内部,而require函数只运行一次,并且无法恢复require函数内部的内容。
so, hello must be outside from require stament if u want call hello whenever you need it. 因此,如果您想在需要时致电hello,则hello必须位于require stament之外。
You could also use the dojo module "On" : 您还可以使用dojo模块“ On”:
Add this to your require init : 将此添加到您的需求init中:
require([...,"dojo/on",...], function(...,on,...) {
Now code your event handler (assuming you add an id to your button) : 现在编写事件处理程序(假设您向按钮添加一个id):
<button type="submit" class="searchButton" id="searchButton">Search</button>
on(dojo.byId('searchButton'), 'click', function(evt) {
alert('hello');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.