You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md
+29-25Lines changed: 29 additions & 25 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -460,33 +460,35 @@ IJSRuntime JS { get; set; }
460
460
461
461
## Create an instance of a JS object using a constructor function
462
462
463
-
Create an instance of a JS object using a constructor function and get the <xref:Microsoft.JSInterop.IJSObjectReference>/<xref:Microsoft.JSInterop.IJSInProcessObjectReference> .NET handle for referencing the instance with the following API.
463
+
Create an instance of a JS object using a constructor function and get the <xref:Microsoft.JSInterop.IJSObjectReference>/<xref:Microsoft.JSInterop.IJSInProcessObjectReference> .NET handle for referencing the instance with the following API:
464
464
465
-
.NET API call examples for `InvokeNewAsync`/`InvokeNew` in this section use the following JS constructor function example (`TestClass`):
465
+
*`InvokeNewAsync`
466
+
*`InvokeNew`
467
+
468
+
Examples in this section demonstrate the API calls with the following `TestClass` with a constructor function (`constructor(text)`):
466
469
467
470
```javascript
468
471
classTestClass {
469
-
constructor(text) {
470
-
this.text= text;
471
-
}
472
+
constructor(text) {
473
+
this.text= text;
474
+
}
472
475
473
-
getTextLength() {
474
-
returnthis.text.length;
475
-
}
476
+
getTextLength() {
477
+
returnthis.text.length;
478
+
}
476
479
}
477
480
478
-
window.jsInteropTests= {
481
+
window.jsInterop= {
479
482
TestClass: TestClass
480
483
};
481
484
```
482
485
483
486
### Asynchronous `InvokeNewAsync`
484
487
485
-
Use `InvokeNewAsync(string identifier, object?[]? args)` on <xref:Microsoft.JSInterop.IJSRuntime> and <xref:Microsoft.JSInterop.IJSObjectReference> to invoke the specified JS constructor function asynchronously. The function is invoked with the `new` operator. In the following example, `jsInteropTests.TestClass`is a constructor function, and `classRef` is an <xref:Microsoft.JSInterop.IJSObjectReference>.
488
+
Use `InvokeNewAsync(string identifier, object?[]? args)` on <xref:Microsoft.JSInterop.IJSRuntime> and <xref:Microsoft.JSInterop.IJSObjectReference> to invoke the specified JS constructor function asynchronously. The function is invoked with the `new` operator. In the following example, `jsInterop.TestClass`contains a constructor function, and `classRef` is an <xref:Microsoft.JSInterop.IJSObjectReference>.
@@ -495,11 +497,11 @@ An overload is available that takes a <xref:System.Threading.CancellationToken>
495
497
496
498
### Synchronous `InvokeNew`
497
499
498
-
Use `InvokeNew(string identifier, object?[]? args)` on <xref:Microsoft.JSInterop.IJSInProcessRuntime> and <xref:Microsoft.JSInterop.IJSInProcessObjectReference> to invoke the specified JS constructor function synchronously. The function is invoked with the `new` operator. In the following example, `jsInteropTests.TestClass`is a constructor function, and `classRef` is an <xref:Microsoft.JSInterop.IJSInProcessObjectReference>.
500
+
Use `InvokeNew(string identifier, object?[]? args)` on <xref:Microsoft.JSInterop.IJSInProcessRuntime> and <xref:Microsoft.JSInterop.IJSInProcessObjectReference> to invoke the specified JS constructor function synchronously. The function is invoked with the `new` operator. In the following example, `jsInterop.TestClass`contains a constructor function, and `classRef` is an <xref:Microsoft.JSInterop.IJSInProcessObjectReference>.
The special overload `IJSObjectReference.GetValueAsync<TValue>()` doesn't take an identifier and simply returns the value of the object. `TValue` is a model of the object's properties that the caller is interested in.
535
540
536
541
Use `SetValueAsync<TValue>(string identifier, TValue value)`to update the value of the specified JS property asynchronously. The property can't be a `get`-only property. If the property isn't defined on the target object, the property is created. In the following example, `testObject.num` is set to 20, and `num2` is created with a value of 30 on `testObject`:
Use `SetValue<TValue>(string identifier, TValue value)` to update the value of the specified JS property synchronously. The property can't be a `get`-only property. If the property isn't defined on the target object, the property is created. In the following example, `testObject.num` is set to 20, and `num2` is created with a value of 30 on `testObject`:
0 commit comments