Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Call JavaScript functions from .NET methods in ASP.NET Core Blazor

JavaScript function that I want to call from my ASP.NET Core Blazor app:

export function onClick(map) {
    var latLng = null;
    function onMapClick(e) {
        latLng = map.unproject(e.point).toString();
        alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
    }
    map.on('click', onMapClick);
    alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
    return String(yourGlobalVariable);
}

C# code where I'm calling that function:

        string res = await mapModule.InvokeAsync<string>("onClick", mapInstance);

Where mapInstance and mapModule are IJSObjectReference

The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?

I've tried similar code here: http://jsfiddle.net/mufjkv6r/1/ and it works

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The question is: Why in the first alert I'm getting an actual value that I've assigned to latLng variable in the function onMapClick and then I get null outside the function although the variable is global?

First thing I want to say is Jsfiddle contain normal flow of execution, which is executing one line after another.

 var latLng = null;
    function onMapClick(e) {
        latLng = map.unproject(e.point).toString();
        alert("You clicked the map at " + latLng); // Here is "You clicked the map at + actual value of latLng"
    }
    map.on('click', onMapClick);
    alert("You clicked the map at 123 " + latLng); // And then "You clicked the map at + null" instead of latLng variable
    return String(yourGlobalVariable);

but the above code will not call onMapClick function unless you clicked on the map. So the latLng will be null at that point of time.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda