Ng-template is not compatible with two-way data binding in this scenario

Why are my selectedContent and selectedSummary not being displayed (blank)? The text inside the <p> tag is showing up instead.

<ng-template #modalTemplate>
  <div class="scroll-container">
    <div class="left-content">
      <div class="scrollable-text" >
        {{ selectedContent }}
        <p>Lorem ipsum</p>
      </div>
    </div>
    <div class="right-content">
      <div class="scrollable-text">
        {{ selectedSummary }}
        <p>Lorem ipsum</p>
      </div>
    </div>
  </div>
</ng-template>

In my .ts file, I have a method that opens a modal when clicking on inspect. In this method, I set the content and summary, pass it to my modalService which then appends the modal to the body and displays it.

  onInspectTranscription(modalTemplate: TemplateRef<any>, content: string, summary: string) {
    console.log('On inspect');
    this.selectedContent = content;
    this.selectedSummary = summary;
    console.log('Content', this.selectedContent);

    this.modalService
      .open(modalTemplate, { size: 'lg', title: 'Foo' })
      .subscribe((action) => {
        console.log('modalAction', action);
      });
  }

I have also declared these two properties as follows:

selectedContent = 'NO CONTENT'; selectedSummary = 'NO SUMMARY';

When I move everything outside of the ng-template, the selectedContent and selectedSummary are displayed, but inside the ng-template, only hard-coded strings show up.

I have tried several troubleshooting steps at this point. I checked my SCSS for any issues, looked for errors in the console, and verified that the properties are set with the correct values (if not, they should display default values).

THE COMPLETE HTML STRUCTURE

<app-page-layout>
  <div class="content-layout">
    <h1 id="page-title" class="content__title">Transcriptions</h1>
    <div class="content__body">
      <ul class="accordion">
        <ng-container *ngFor="let item of transcriptions; let first = first">
          <li class="accordion-list">
            <div class="accordion-item" (click)="selectItem(item.id)">
              <app-transcription [transcriptionItem]="item" [isChecked]="first"></app-transcription>
              <i class='bx bx-chevron-right chevron-right-icon' *ngIf="item.id === selectedItemId"></i>
              <!-------MENU------->
              <div class="transcription-menu">
                <div class="menu-item" (click)="onDeleteTranscription(item.id)">
                  <i class='bx bx-trash'></i>
                  <span>Delete</span>
                </div>
                <div class="menu-item" (click)="onInspectTranscription(modalTemplate, item.content, item.summary)">
                  <i class='bx bx-book-open'></i>
                  <span>Inspect</span>
                </div>
              </div>
              <!------------------>
            </div>
          </li>
        </ng-container>
      </ul>
    </div>
  </div>
</app-page-layout>

<ng-template #modalTemplate>
  <div class="scroll-container">
    <div class="left-content">
      <div class="scrollable-text" >
        {{ selectedContent }}
        <p>Lorem ipsum</p>
      </div>
    </div>
    <div class="right-content">
      <div class="scrollable-text">
        {{ selectedSummary }}
        <p>Lorem ipsum</p>
      </div>
    </div>
  </div>
</ng-template>

Answer №1

If you're seeing the modal open correctly and the content within the <p> tag is being shown, here are a few troubleshooting steps that might be helpful:

  1. Double check that both selectedContent and selectedSummary are accessible to your block.
  2. You could experiment with manual change detection by using
    ChangeDetectorRef.detectChanges()
    within the onInsepctTranscription.
  3. Alternatively, consider utilizing BehaviorSubject from rxjs for those variables.

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

The create document feature seems to be malfunctioning for some reason. Any ideas why it's not working properly in Firebase with Angular

I've been attempting to submit user data to the Firebase Firestore database, but I'm experiencing issues with the function that is supposed to create a new collection. Despite trying different methods, none of them seem to be working for me. I ha ...

Angular Material Design Components is displaying the error message "The mixin mdc-top-app-bar-fill-color does not exist"

Currently, I'm working with angularCLI and angular-MDC and I am trying to implement the second color from my styles.scss file. I came across this syntax on the following website which is mdc-top-app-bar-fill-color($color). When I tried using it, I re ...

Angular project is showing a unique error that says '$localize' is undefined according to Eslint

I successfully implemented localization in my Angular 15 project. However, I encountered an issue with linting for .ts files: error '$localize' is not defined no-undef Here's the configuration in my .eslintrc.json file: { "root": true, ...

AngularJS is encountering issues with dependency injections when using WebStorm and TypeScript

A TypeScript AngularJS component: class MyComponentCtrl { static $inject = ['MyService']; constructor(private MyService) { MyService.testfn(55); // No error in typescript } } class MyComponent implements ng.IComponentOptions { ...

When typing declarations are used, they clarify whether the entity being referenced is an Object or

I am currently working on aligning the Dockerode run typings to match the actual implementation. The issue arises when I invoke run as TypeScript consistently identifies the return value as a Promise. It seems that TypeScript is having trouble distinguish ...

Encountering challenges with Angular in creating a dynamically responsive background

I'm struggling to make this website fully responsive and adapt to various screen resolutions. The background images I'm using are high-resolution, but they don't seem to cover the entire page. CSS : @media screen and (max-width: 600px) { ...

Generating fake data from MongoDB to meet TypeScript requirements in Jest testing

Currently, I am in the process of developing TypeScript interfaces for each model that extends mongoose.Document. import mongoose, { Document } from 'mongoose'; export interface IAccount extends Document { _id: mongoose.Types.ObjectId; name ...

Retrieving the selector name from a JSON in Angular 2

I'm looking to create a dynamic form where element details will be sourced from JSON data. This is the JSON structure I have: { "FormElements": [ { "selectorName": "text-input", "id": "", "class": "location", "nam ...

Error TS2322: Cannot assign type 'Observable<{}>' to type 'Observable<Hero>'

In the hero.service.ts constructor: @Injectable() export class HeroService { private _heroObserver: Observer<Hero>; hero$: Observable<Hero>; public errorMessage: string; constructor (private http: Http) { this.hero$ = new Obs ...

Expanding IntelliSense and helpful tooltips in VSCode for JavaScript and TypeScript by utilizing Node.js for a deeper understanding

As a beginner in programming, specifically in JS/TS, I've been experimenting with node.js and have encountered a puzzling issue with the IntelliSense or 'helptext' feature in VSCode. For instance, when attempting to use fs.open(), I receive ...

What could be causing my Angular2 component to not properly use my template?

I have two components that I am working with. The first component is: import {Component} from 'angular2/angular2'; import {Navbar} from './navbar'; @Component({ selector: 'app' template: `<div class="col-md-12"> ...

Error in Typescript: Attempting to access the property 'set' of an undefined value

Currently, I am in the process of setting up a basic example of push notifications on Android using Nativescript and Typescript. Although my code may seem a bit messy, I am struggling with properly rewriting "var Observable = require("data/observable");" a ...

Creating multiple routes within a single component in Angular without triggering unnecessary redraws

Within the ChildComponent, there is a reactive form that allows for data entry. Upon saving the filled form, the route should be updated by appending an id to the current route. Currently, when saving, the component renders twice and causes screen flicke ...

How to create a beautiful grid layout using @angular/flex-layout, @angular/material, and mat-card to showcase stunning images

I have created a basic layout that looks like this: Cell 0 Cell 1 Cell 2 <!-- these are not to appear in actual render __________________________________ | Image 0 | Image 1 | Image 2 | |----------| Image 1 |----------- | text here| ...

Tips for making the editor automatically focus on the expansion panel content when the expansion panel is opened in Angular

I need assistance with focusing the Quill editor when expanding an expansion panel in my project. The expansion panel has a header that reads "Write a Response", and upon opening it, the Quill editor should be focused. <mat-expansion-panel #panel1> & ...

Unit testing in Angular 4 with Jasmine Spy: The expectation was for 'New text' but received undefined

I have a simple function in my app.component.ts that is meant to modify a parameter, and I am trying to test this function using a spy. However, for some reason, my changeText function always returns undefined. Can you help me identify what I might be doin ...

Default exports are not supported in TypeScript

I'm encountering issues with my Laravel + Vite + Vue 3 project. I followed the installation instructions in the documentation and everything works fine when the project is separated from Laravel and Vite. However, I'm facing a problem where TypeS ...

ngx-bootstrap modal failing to access the properties of my component

I am currently utilizing ngx-bootstrap with bootstrap 3.3.7 and facing an issue with the modal service. Although the modal dialog opens successfully, only the static content is displayed. None of the dynamic content that I pass to the service seems to work ...

Looking for Angular 4 subscription to run every time the view is displayed

I'm still learning Angular and experimenting with a way for a child component to notify the parent component when a job is running and when it's completed, so it doesn't have to rerun if the child component is relaunched. To achieve this, I& ...

Top method for extracting a correlation matrix using an API endpoint

I am currently storing correlations on Google Firebase in a structure that resembles the following: {a: {a: 1.0, b: 0.6, c: -0.3, ...}, b: {a: 0.6, b: 1.0, c: -0.5, ...}, ...} My goal is to efficiently retrieve a complete correlation matrix while also hav ...