Unusual Interactions between Angular and X3D Technologies

There is an unusual behavior in the x3d element inserted into an Angular (version 4) component that I have observed.

The structure of my Angular project is as follows:

x3d_and_angular/
    app/
        home/
            home.component.css
            home.component.html
            home.component.ts
            index.ts
        viewer/
            viewer.component.css
            viewer.component.html
            viewer.component.ts
            index.ts
        app.component.html
        app.component.ts
        app.module.ts
        app.routing.ts
        main.ts
    app.css
    index.html
    package.json
    red_box.x3d
    sysytemjs.config.js

I included the x3dom library in both package.json and sysytemjs.config.js.

This is how my index.html looks like:

<!DOCTYPE html>
<html>
<head>
    <base href="/" />
    <title>X3D and Angular Integration</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- bootstrap css -->
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <link rel='stylesheet' type='text/css' href='http://www.x3dom.org/download/x3dom.css'>

    <!-- application css -->
    <link href="app.css" rel="stylesheet" />

    <!-- polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>

    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
        System.import('app').catch(function (err) { console.error(err); });
    </script>

    <script>
        function show_inline_url(){
            console.log(document.getElementById("inline_element"));
        }
    </script>
</head>
<body>
    <app>Loading ...</app>
</body>
</html>

The X3D file (red_box.x3d) that displays a small red box has the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.3//EN" "http://www.web3d.org/specifications/x3d-3.3.dtd">
<x3d>
  <head>
    <meta name="title"/>
  </head>
  <Scene>
    <Shape id="shape_id">
        <appearance> 
            <material diffuseColor='1 0 0'></material>
        </appearance>       
        <box></box> 
    </Shape>
  </Scene>
</x3d>

Here is the code for the home component:

  • home.component.html:

    <div>
        <a [routerLink]="['/viewer']">X3D Viewer</a>
    </div>
    
  • home.component.ts:

    import { Component, OnInit } from '@angular/core';
    
    @Component({
        moduleId: module.id,
        templateUrl: 'home.component.html',
        styleUrls: ['home.component.css'],
    })
    
    export class HomeComponent{
        constructor() { }
    }
    

The issue arises in the viewer component. Below are the files associated with this component:

  • viewer.component.html:

    <div>
        <x3d width='600px' height='400px'> 
            <scene>
                <inline id="inline_element" [url]="x3d_filename"> </inline>
            </scene>
            <a [routerLink]="['/']"><span>BACK</span></a>
        </x3d> 
    </div>
    
  • viewer.component.ts:

    import { Component, OnInit } from '@angular/core';
    
    declare var System: any;
    
    @Component({
        moduleId: module.id,
        templateUrl: 'viewer.component.html',
        styleUrls: ['viewer.component.css'],
    })
    
    export class ViewerComponent implements OnInit{
    
        x3d_filename: string = "red_box.x3d";
    
        constructor() { 
            this.importX3d();
        }
    
        ngOnInit(){
            if(window["x3dom"] != undefined){
                window["x3dom"].reload();
            }
        }
    
        importX3d():void{        
            System.import('x3dom').then(() => { 
                console.log('loaded x3d');
            }).catch((e:any) => {
                console.warn(e);
            })
        }
    }
    

The routing of my Angular application goes from the home component to the viewer component and vice versa. These routes are defined in the app.routing.ts file:

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/index';
import { ViewerComponent } from './viewer/index';

const appRoutes: Routes = [
    { path: '', component: HomeComponent},
    { path: 'viewer', component: ViewerComponent},

    // otherwise redirect to home
    { path: '**', redirectTo: '' }
];

export const routing = RouterModule.forRoot(appRoutes);

Upon refreshing the website, when accessing the viewer component for the first time, the scene appears empty. However, subsequent visits to this component through routing (without refreshing) result in the box being rendered inside the scene.

Replacing the inline tag in the viewer.component.html file with

<inline url="red_box.x3d"> </inline>
eliminates this issue (the box is rendered even on the first visit after a refresh).

If anyone has suggestions on how to resolve this peculiar behavior, I would greatly appreciate it. I am relatively new to Angular and web development, so any assistance is welcomed. Thank you.

Answer №1

Following Sara Jones's advice, the issue can be resolved by substituting [url] with [attr.url] in the document viewer.component.html. Furthermore, alterations need to be made to the file viewer.component.ts, and it should now read as shown below:

import { Component, AfterViewInit, OnDestroy } from '@angular/core';

declare var System: any;

@Component({
    moduleId: module.id,
    templateUrl: 'viewer.component.html',
    styleUrls: ['viewer.component.css'],
})

export class ViewerComponent implements AfterViewInit, OnDestroy{

    x3d_filename: string = "blue_sphere.x3d";

    constructor() { 
        this.importX3d();
    }

    ngAfterViewInit(){
        if(window["x3dom"] != undefined){
            window["x3dom"].reload();
        }
    }

    importX3d():void{        
        System.import('x3dom').then(() => { 
            console.log('loaded x3d');
        }).catch((e:any) => {
            console.warn(e);
        })
    }

    ngOnDestroy(){
        System.delete(System.normalizeSync('x3dom'));
    }

}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the contents of JSON data using json-server

In my database file db.json, I have the following data: { "cases":{ "TotalCount":1, "StartingPageNumber":1, "Data":[ { "Id":1, "CaseNumber":& ...

Can we rely on the security of Ionic 4 secure storage encryption?

I'm currently developing an application that necessitates the user to be in close proximity to a specific GPS location. At present, I am obtaining their location every 30 seconds, transmitting it to my server, checking if they are near the desired loc ...

The initial function is executed only after the second function has completed, as it relies on the

For a small project of mine, I've been attempting to load JSON data. However, the issue arises when the loadDefs function is executed before checking if file_data has been modified. loadDefs(file_path:any) { let file_data:string = '&a ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

Having trouble determining the reason why the routing isn't functioning properly

Currently on a journey to grasp the ins and outs of angular2. After diving into tutorials and doing some research, I've made the decision to build a working demo incorporating all common features. I am currently struggling with implementing routing b ...

When attempting to compile my Angular project using the command ng build --prod, I encountered a server error stating that the document

Everything was running smoothly when I was working on my local machine, but once I uploaded the files generated from ng build --prod to the server, a problem arose. Now, whenever I try to route via a button in my components, an error appears. When I clic ...

The client PC running the Ionic app encountered an error while trying to load chunk 'XY'

I'm currently working with Ionic 3 and lazy loading. Everything is running smoothly on 12 different PCs. However, I encountered an issue on one PC where it started displaying the error message "Loading chunk 7 failed," sometimes with numbers like 43 o ...

Manipulating Angular and Typescript to utilize the method's parameter value as a JavaScript object's value

I am currently working with Ionic, Angular, and Typescript, attempting to dynamically set the value of a location based on the parameter passed from a method. Here is the relevant code snippet: async fileWrite(location) { try { const result = a ...

Angular generating JSON data

I am trying to figure out how to extract and display the short_title from the JSON object with the "id": 28. I want to use Angular to render this title within an HTML page. { "content": [ { "id": 29, "short_title": "Flow", ...

Should loaders be utilized in an Angular application?

Webpack configuration allows the use of various loaders, such as file-loader, html-loader, css-loader, json-loader, raw-loader, style-loader, to-string-loader, url-loader, and awesome-typescript-loader. Does Angular have built-in knowledge of loaders with ...

Is there a way to adjust the height of mat-sidenav-content to be 100%?

I'm having trouble scrolling down my mat-sidenav-content to reach the bottom where my pagination is located. When I try using fullscreen on mat-sidenav-container, my mat-toolbar disappears. How can I adjust my mat-sidenav-content based on the content? ...

Leveraging --expose-gc for TypeScript when working with ts-node or tsx

It appears that neither ts-node nor tsx has support for the --expose-gc flag, leading to the garbage collector object global.gc being undefined. What is the recommended way to handle memory cleanup in TypeScript? ...

Issue with Angular ngModel not syncing with variable changes

Currently using Angular 4 and Typescript, I have a table containing <select> elements in my template: <tr *ngFor="let task of tasksDetails"> <td>{{task.name}}</td> <td> <select class="form-control" [(ngMode ...

Storing data in Angular 2's ngFor loop for later usage

I have a list of different types created by a for loop. Each type in the list has a delete button next to it, allowing users to remove that specific item. Clicking on the delete button opens a modal where the user can confirm the deletion. To avoid having ...

Guide on entering text into an Angular input field with Selenium in Python after navigating tabs

After switching tabs, I am attempting to enter text into an Angular input field. However, I keep encountering the following errors: AttributeError: 'tuple' object has no attribute 'send_keys' or ElementClickInterceptedException or NoS ...

Using Symbol.iterator in Typescript: A step-by-step guide

I have decided to upgrade my old React JavaScript app to React Typescript. While trying to reuse some code that worked perfectly fine in the old app, I encountered errors in TS - this is also my first time using TS. The data type I am exporting is as foll ...

Issues with user-generated input not properly functioning within a react form hook

After following the example provided here, I created a custom input component: Input.tsx import React from "react"; export default function Input({label, name, onChange, onBlur, ref}:any) { return ( <> <label htmlF ...

The CSS "mask" property is experiencing compatibility issues on Google Chrome

Hey there! I've recently developed a unique progress bar component in React that showcases a stunning gradient background. This effect is achieved by utilizing the CSS mask property. While everything runs smoothly on Firefox, I'm facing a slight ...

What implications does dependency injection have for performance in Angular?

Our Angular 2/4 application is quite extensive, utilizing reactive forms with a multitude of form controls. I'm wondering about the impact of injecting a ChangeDetectorRef instance into approximately 200 form control components. Will there be a notic ...

Angular expands the HTML of the parent component

Although this question may be old, I am still struggling to find a straightforward answer and it just doesn't make sense to me. I have a main component with its own HTML and several components that inherit from it. Here is what I am trying to achiev ...