Reputation: 822
I am new to Blazor Webassembly and find out how to call a javascript function from .NET code. My question is: Is it possible to read and write also a javascript variable from .NET. Here an example:
Javascript code loaded into page:
var Namespace1 = {
prop1: 42,
func1: function () {
return 42;
}
}
from .NET I can call the function Namespace1.func1:
@code {
//private int prop1 { get; set; } = 1;
private int ret1 { get; set; }
override protected async Task OnInitializedAsync() {
// Error: The value 'window.Namespace1.prop1' is not a function.
// ret1 = await JSRuntime.InvokeAsync<int>("Namespace1.prop1");
ret1 = await JSRuntime.InvokeAsync<int>("Namespace1.func1");
// return base.OnInitializedAsync();
}
}
Is it possible to access also Namespace1.prop1 from .NET
Upvotes: 0
Views: 3049
Reputation: 5481
That's not possible currently as IJsRuntime
only exposes methods that invoke javascript functions. Details here on Microsoft docs
But you can write a js function and return a property:
var Namespace1 = {
prop1: 42,
func1: function () {
return prop1;
}
}
Upvotes: 5