Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

Using the Document Viewer in Angular Routes

This article describes how to use DocumentViewer for Angular in routes by initializing the viewer using the init method on switching the route.

Using the Document Viewer in Angular Routes

The DocumentViewer for Angular is created as a singleton instance on a page. Therefore, the complete control must be re-initialized explicitly when loading a partial view a second time. In Angular, "partial views" are implemented through routes. In this sample, two routes are implemented:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { TextControlComponent } from './textcontrol/textcontrol.component';


const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'textcontrol', component: TextControlComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

The component home and textcontrol are navigated through links on the main app page:

<h1>Main View</h1>

<nav>
  <a routerLink="/home" routerLinkActive="active">Home</a>
  <br>
  <a (click)="initializeViewer()" routerLink="/textcontrol" routerLinkActive="active">
    Document Viewer
  </a>
</nav>

<router-outlet></router-outlet>

Due to the nature of Angular routes, the page is not reloaded when clicking on the navigation links, but a partial views is loaded dynamically into the DOM.

In this case, the DocumentViewer instance needs to be initialized explicitly by calling the init method. In the above code, the (click) event is used to call the initializeViewer method.

function initializeViewer()
{
    TXDocumentViewer.init(
        { containerID: "txViewer", viewerSettings: {
            signatureSettings: {
                "ownerName":"",
                "signatureBoxName":"txsign",
                "signerName":"",
                "signerInitials":"",
                "showSignatureBar":false,
                "uniqueId":"",
                "redirectUrlAfterSignature":""
            },
            userNames: [],
            dock: 1,
            documentData: "SGVsbG8gdGhlcmU=",
            documentPath: "test.docx",
            toolbarDocked: true,
            isSelectionActivated: true,
            showThumbnailPane: true,
            resources: null,
            basePath: "https://backend.textcontrol.com"
            }
        }
    );
}

This way, a new partial view can be loaded and reused including the DocumentViewer.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Angular

Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.

Learn more about Angular

Related Posts

AngularASP.NETJavaScript

Using the Document Editor in SPA Applications using the removeFromDom Method

This article shows how to use the removeFromDom method to remove the Document Editor from the DOM when it is no longer needed. This is useful when the Document Editor is used in a Single Page…


AngularASP.NETJavaScript

Observe When the Reporting Preview Tab is Active Using MutationObserver

This article shows how to observe when the Reporting Preview tab is active using MutationObserver. The Reporting Preview tab is a feature of the TX Text Control Document Editor that allows you to…


AngularASP.NETJavaScript

Building an ASP.NET Core Backend Application to Host the Document Editor and…

This article explains how to create an ASP.NET Core backend application to host the Document Editor and Document Viewer. This backend application is required to provide the required functionality…


AngularASP.NETJavaScript

Reuse Angular Document Editor Instances in Bootstrap Tabs

The initialization time of document editor instances is time-consuming and requires resources on both the server side and the client side. To reuse instances, they can be moved within the DOM.…


AngularASP.NETJavaScript

JavaScript: Avoid Flickering and Visual Updates by Grouping Undo Steps

The JavaScript API can be used to group several steps into one undo action that can be undone with a single undo call. Additionally, those groups are visually updated at once to avoid screen…