在blazor(服务器)组件的标记中,我试图在循环内添加的每个元素上调用 javascript 函数。这有点难以解释......希望这个代码片段有帮助:
@page "/"
@inject IJSRuntime JS
@for(var i = 0; i < 3; i++)
{
ElementReference div;
<div @ref="div">this should get replaced....</div>
JS.InvokeVoidAsync("Test", div, i); @* where Test is defined as: function Test(el, i) { el.innerHTML = i; }*@
}
如果我以错误的方式解决这个问题......什么是正确的方式?
对于更多上下文,这就是我真正想要做的:
@page "/"
@inject IJSRuntime JS
@foreach(var data in jsonItems)
{
ElementReference div;
<div @ref="div"></div>
JS.InvokeVoidAsync("JsonView.renderJSON", data, div); @* https://github.com/pgrabovets/json-view *@
}
@code
{
private readonly IEnumerable<string> jsonItems = new List<string> {"{}", "{}"};
}
如果有帮助,我已经创建了这个 blazor fiddle... https://blazorfiddle.com/s/428ov3ku