Is it possible for transclusion to display content from external sources using *ngIf and <ng-content>?

In my Angular4 Project, I have come across this snippet of code:

<div class="divider"></div>

<ng-content select=".nav-toggle"></ng-content>

Now, I am trying to figure out a way to display the divider only when there is content provided from outside the ng-content tag. Can anyone guide me on how to achieve this? I have searched extensively but couldn't find any relevant information.

Answer №1

There isn't a simple method to retrieve arbitrary projected content

@Component({
    selector: 'item',
    template: `
<div *ngIf="divider" class="divider"></div>
<div #wrapper><ng-content select=".nav-toggle"></ng-content></div>'})
class Item implements AfterContentInit {

    @ViewChild('wrapper') wrapper:ElementRef;

    divider:boolean = false;

    ngAfterContentInit() {
      console.log(this.wrapper.nativeElement.innerHTML); // or `wrapper.text`
      this.divider = !!this.wrapper.nativeElement.children;
    }
}

Check out Access transcluded content

If you are aware that the projected content is from a specific Angular component, or the element contains a specific template variable, you can utilize @ContentChildren() (refer also to angular 2 / typescript : get hold of an element in the template).

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

Having trouble getting Next.js 404 page to function properly with the .tsx extension?

My latest project involved creating a Next.js application using regular JavaScript, which led to the development of my 404 page. 404.js import { useEffect } from "react"; import { useRouter } from "next/router"; import Link from " ...

Is there a way to set up the application that consumes an npm module with a private git url to strictly utilize files exclusively from the module's dist folder?

In my angular application, I encountered an issue with angular-cli not supporting the creation of a library. To work around this, I opted to use the popular git project found at https://github.com/jvandemo/generator-angular2-library for creating my library ...

Rendering a sanitized string with interpolation in Angular 2

After receiving the string below: "Today's product of the day is {{product_code}} !" I took this string, sanitized it to bypass security restrictions using HTML this.DomSanitizer.bypassSecurityTrustHtml(str) and inserted it into my template using ...

CORS regulations are preventing access

I have set up an Express server to run my Angular app for server-side rendering purposes. Initially, everything works fine when I make a request from the application. However, an issue arises when I navigate to another page and then return to the previous ...

Navigate to a different page using Angular2 routing

Looking for guidance on using redirect in the new Angular 2 router. Specifically interested in examples similar to 'redirectTo' from the beta version. Any demos on 'plnkr.co' would be greatly appreciated! ...

New feature: Mat select placeholder color change without needing required field validation

https://i.sstatic.net/QczIz.png When the page initially loads, all controls (such as city, state, etc.) have white text color as shown in Image 1. I want to change the text color to red for all controls upon loading the page, resulting in a look similar t ...

Converting JSONSchema into TypeScript: Creating a structure with key-value pairs of strings

I am working with json-schema-to-typescript and looking to define an array of strings. Currently, my code looks like this: "type": "object", "description": "...", "additionalProperties": true, "items": { "type": "string" ...

Having trouble with Angular 2+/NodeJS/Express routing after refreshing the page?

Initially, I believed this issue to be specific to Heroku, but it persists even when running the application locally with NodeJS. The main page of my Angular app loads perfectly, and the routes function correctly when navigating through the links provided ...

After a duration of 4 minutes, an ERR_EMPTY_RESPONSE is thrown in response to the

My angular 5 node app involves sending a request to the server and waiting for a response. There are instances where the response takes around 5-6 minutes to arrive. However, an ERR_EMPTY_RESPONSE error is triggered by the http method after just 4 minutes ...

Issues with manipulating state using TypeScript Discriminated Unions"Incompatibility with setState

Imagine having a unique type structure like the one shown below: export type IEntity = ({ entity: IReport setEntity: (report: IReport) => void } | { entity: ITest setEntity: (test: ITest) => void }); Both the entity and setEntity fun ...

Troubleshooting fastify library errors related to ajv validation

Every time I try to build my TypeScript code, I encounter these errors: The following errors are showing up in my TypeScript code: 1. node_modules/@fastify/ajv-compiler/types/index.d.ts(1,10): error TS2305: Module 'ajv' has no exported member ...

Cannot see the template on the Angular Typescript component

After encountering and resolving this issue: AngularJS directive not displaying the template I decided to experiment with an Angular component and TypeScript, but unfortunately, I can't seem to make it work. The component refuses to display. This is ...

Ensure that the string functions as the primary interface

I'm working with an interface that looks like this interface Cat { color: string, weight: number, cute: Boolean, // even though all cats are cute! } Currently, I need to accomplish something similar to this const kitten: Cat = ... Object. ...

What is the reason behind the inability of the Angular/TypeScript HTTP GET method to accept a JSON object as a parameter

EXAMPLE 1 passJSONObj(){ let name = 'user names', let email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d7a2a4b2a5b2bab6bebb97b3b8bab6beb9f9b2afa3">[email protected]</a>&ap ...

TS: Utilizing a generic parameter in an overloaded function call

This piece of code encapsulates the essence of what I'm trying to achieve more effectively than words: function A(a: string): string; function A(a: number): number; function A(a: any) { return a; } function B<T extends number | string>(arg: T): ...

`Why am I unable to achieve the proper body shape with Angular?`

When using Angular to retrieve registration information from an HTML form and then posting it to an API path, everything seems to be working fine. However, the word "work" is being printed and I am unable to get the value in the body. I have tested the POS ...

Angular: Showing the Gap between Basic Elements within an Array in the Template

I've been working on displaying a nested JSON object in a table, where there is an array of strings like obj.someKey = ['a','b','c']. Currently, I am directly showing the array content in the td tag of the table. <td ...

Angular import definitions corresponding to Font Awesome classes

https://fontawesome.com/icons/ When looking for an icon, the website provides a class to add to the HTML code. However, when using Angular, it is necessary to use icon definitions. For instance: <i class="fa-solid fa-star"></i> Wo ...

Navigating Between Components in Angular: How to Redirect from One Component to

As I delve into learning angular Routing and Navigation, I find myself stuck at the navigation part. This is the structure of my Angular Components: AppComponent -LandingPage -WhatEver1 -Home -Price -Sales -WhatEver2 -Blog ...

What is the process of creating the /dist folder in an unreleased version of an npm package?

Currently working on implementing a pull request for this module: https://github.com/echoulen/react-pull-to-refresh ... It seems that the published module generates the /dist folder in the package.json prepublish npm script. I have my local version of the ...