简体   繁体   English

调用 JS function 的 .NET 方法

[英].NET method that invoke JS function

I have a function as below in index.js, which is invoked from navbar.razor我在 index.js 中有一个 function,如下所示,它是从 navbar.razor 调用的

                    // gets the ID of button clicked - always gets the correct id
                    var BtnID = null 
                    var buttonElements = document.querySelectorAll('#items button');
                    for (var i = 0; i < buttonElements.length; i++) {
                        buttonElements[i].addEventListener('click', function () {                      
                            BtnID = this.getAttribute('id');
                        });
                    };
                    

                   // invoked from navbar.razor
                   // here is the problem, it works initially but does not work or set new ID when I switch from (page2-> page1-> page2). Remembers the old ID that was clicked before switching page.
                   window.getValues = function (val1, val2) {
                       yAxis[BtnID].setExtremes(val1, val2);                         
                 }

window.getValues works well initially. window.getValues 最初运行良好。 But once I switch pages, like page2 to page1 and come back again to page2(which is where this functionality is applied), it remembers only the old ID but not the new button ID that was clicked or does not take the new ID passed(BtnID) as shown above in index.js.但是一旦我切换页面,比如 page2 到 page1 并再次返回到 page2(这是应用此功能的地方),它只记住旧 ID,而不是单击的新按钮 ID,或者不接受新 ID 传递( BtnID)如上面 index.js 中所示。 May I now what would be the problem.请问我现在会有什么问题。 Thank you.谢谢你。

Navbar.razor:导航栏.razor:

    <div id="items">
      <button id="0" @onclick=OpenDialog1>one</button>
      <button id="1" @onclick=OpenDialog2>two</button>
    </div>
                                  
    @code{
    private MyData model1 = new MyData { val1= 0, val2= 0 };
    private MyData model2 = new MyData { val1= 0, val2= 0 };
  
    private IModalDialog? modalDialog;

    private async Task OpenDialog1()
    {
        var request = new ModalRequest { InData = this.model1 };
        if (this.modalDialog is not null)
            await modalDialog.ShowAsync<MyForm>(request);      
        await JSRuntime.InvokeVoidAsync("getValues", this.model1.val1, this.model1.val2);
    }

    private async Task OpenDialog2()
    {
        var request = new ModalRequest { InData = this.model2};
        if (this.modalDialog is not null)
            await modalDialog.ShowAsync<MyForm>(request);      
        await JSRuntime.InvokeVoidAsync("getValues", this.model2.val1, this.model2.val2);
    }

} }

Your getValues function is becoming a closure.您的getValues function 正在成为闭包。 A closure is a function that references variables in the outer scope from its inner scope.闭包是一个 function,它从其内部 scope 引用外部 scope 中的变量。 The closure preserves the outer scope inside its inner scope.闭合将外部 scope 保留在其内部 scope 内。

You should place the code that gets the button ID inside the function:您应该将获取按钮 ID 的代码放在 function 中:

window.getValues = function (val1, val2) {
    var BtnID = null; 
    var buttonElements = document.querySelectorAll('#items button');
    for (var i = 0; i < buttonElements.length; i++) {

        buttonElements[i].addEventListener('click', function () {                      
            BtnID = this.getAttribute('id');
        });
    };
  
    yAxis[BtnID].setExtremes(val1, val2);                         
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM