You are unable to link to <custom directive selector> because it is not recognized as a valid property of 'div'

I am currently working on a project in StackBlitz, and you can find the link here: https://stackblitz.com/edit/angular-fxfo3f?file=src/directives/smooth-height.directive.ts

I encountered an issue:

Error in src/components/parent/parent.component.html (2:6)
Can't bind to 'appSmoothHeight' since it isn't a known property of 'div'.

I'm struggling to figure out what I'm missing.

smooth-height.directive.ts:

import {
  Directive,
  ElementRef,
  HostBinding,
  Input,
  SimpleChanges,
} from '@angular/core';

@Directive({
  selector: '[appSmoothHeight]',
  standalone: true,
})
export class SmoothHeightDirective {
  @Input() smoothHeight: boolean;
  pulse: boolean;
  startHeight: number;

  constructor(private element: ElementRef) {}

  @HostBinding('@grow')
  get grow() {
    return { value: this.pulse, params: { startHeight: this.startHeight } };
  }

  setStartHeight() {
    this.startHeight = this.element.nativeElement.clientHeight;
  }

  ngOnChanges(changes: SimpleChanges) {
    console.log(changes);
    this.setStartHeight();
    this.pulse = !this.pulse;
  }
}

parent.component.ts:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SmoothHeightDirective } from '../../directives/smooth-height.directive';
import { smoothHeight } from '../../../animations';
import { TestComponent } from '../test/test.component';

@Component({
  selector: 'app-parent',
  templateUrl: './parent.component.html',
  styleUrls: ['./parent.component.css'],
  standalone: true,
  animations: [smoothHeight],
  imports: [TestComponent, SmoothHeightDirective, CommonModule],
})
export class ParentComponent {
  longContent: boolean = false;

  toggleContent() {
    this.longContent = !this.longContent;
  }
}

parent.component.html:

<button (click)="toggleContent()">Toggle Content</button>
<div [appSmoothHeight]="longContent"></div>

Answer №1

This question has already been addressed here: Angular2 Can't bind to DIRECTIVE since it isn't a known property of element

Essentially, the @input within the custom directive needs to have the same name as its selector. For example:

@Directive({
  selector: '[appSmoothHeight]', 
})
export class SmoothHeightDirective {
  @Input() appSmoothHeight: boolean; 
// ...

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

NGINX: Serving as a proxy exclusively for upstream requests

I have a scenario that I'm hoping to get some assistance with. The setup involves hosting multiple Angular projects on nginx using a single URL with path extensions to distinguish between the sites. Here's an example snippet of the configuration: ...

Creating a default option in a Select tag with React when iterating over elements using the map method

After learning that each element in the dropdown must be given by the Option tag when using Select, I created an array of values for the dropdown: a = ['hai','hello','what'] To optimize my code, I wrote it in the following ...

Typescript: uncertain about the "declaration: true" guideline

Let's say I have a app.ts file: interface IApp {} export class App implements IApp {} If I set declaration to true in tsconfig.json, an error will occur: error TS4019: Implements clause of exported class 'App' has or is using private name ...

Is it possible for Angular version 15 to function without needing to migrate to material

Can anyone clarify whether material migration is necessary when upgrading from Angular v14 to v15? The Angular upgrade guide mentions that old(v14) material modules can still be used by utilizing legacy modules, so is it mandatory to migrate? "In the new ...

Utilizing Ngrx store for Reacting form validation with the integration of asynchronous validation

I'm currently working on an Angular 8 project where I aim to showcase form errors through NgRx store while utilizing reactive forms with a custom asynchronous validator. login.component.ts @Component({ selector: 'auth-login', templateU ...

Ways to bypass observance of an empty array?

I am utilizing the combineLatest operator: combineLatest(...this.filtersList.map((f) => f.filtersChanges)).subscribe((data) => { console.log(data); }); This operator retrieves the latest emitted changes in each stream - f.filtersCh ...

Welcome to Ng-Zorro, your destination for all things innovative and cutting

After including the NG-Zorro library in my project, every page I navigate to within my App displays only the NG-Zorro logo and the message "You have Arrived". I'm curious about removing this logo/text so that I can properly view my pages. I referred ...

How can I prevent right-clicking with Ctrl+LeftMouseClick in Firefox on MacOS?

I'm looking to implement a shortcut using Ctrl+LeftMouseClick in my React project. It functions perfectly on Chrome on my Mac, but in Firefox the shortcut initiates a right mouse click (event.button = 2). I believe this may be due to MacOS's Rig ...

One way to incorporate type annotations into your onChange and onClick functions in TypeScript when working with React is by specifying the expected

Recently, I created a component type Properties = { label: string, autoFocus: boolean, onClick: (e: React.ClickEvent<HTMLInputElement>) => void, onChange: (e: React.ChangeEvent<HTMLInputElement>) => void } const InputField = ({ h ...

What is the reason that Jest is not able to spy on the function?

A custom Hook was developed with only one function being imported. Ensuring this function is called with the correct arguments is crucial. import { IsValueAlreadyRegistered } from "../../entities/registration/actions"; export const useForgetPass ...

Is there a way to transform time into a percentage with the help of the moment

I am looking to convert a specific time range into a percentage, but I'm unsure if moment.js is capable of handling this task. For example: let start = 08:00:00 // until let end = 09:00:00 In theory, this equates to 100%, however, my frontend data ...

Could one retrieve the value of a type and save it as a constant?

Can I achieve something similar to this: type YesType = true; const myVar = GetTypeValue<YesType>(); // In this instance, the value true is assigned Is it feasible to assign other fixed values to constant variables like in C++? ...

Exploring various queries in Firestore

Does anyone know if there is a way to create a sentence similar to this one: return this.db.collection('places', ref => ref.where("CodPais", "<>", pais)).valueChanges(); I have tried using != and <> but neither seem to be valid. Is the ...

Typescript types for the Google Calendar API

Is there anyone out there who can confirm whether the google Calendar API (via the npm package googleapis) for node.js or browser supports types that can be utilized in TypeScript? This would allow for a more strongly typed approach in projects using node ...

Is there a way to access the actionsObserver value from the Angular state?

When attempting to view state data using the code line this.store.select(selectUser), I am able to see the data as expected under actionsObserve. However, I am facing difficulty reading the data in actionsObserve with this line: this.store.select(selectUs ...

What are some ways to expand the width of a MaterialUI form control if no value has been chosen?

I am currently working on a project where I need a dropdown menu component with specific selections. However, the layout appears to be cramped and I'm struggling to adjust the width. Additionally, I've been unsuccessful in changing the font size ...

Tips for testing an Angular 2 component that integrates a third-party JavaScript library

Seeking guidance on how to properly test the component below that utilizes a 3rd party JavaScript library - import * as Leaflet from "leaflet"; export class GeoFencingComponent { map: any; constructor() { this.map = Leaflet ...

Following the Angular 6 update, a new error message "Error: Expected to find an ngsw-config.json configuration file" appears when running the ng build --prod command

After completing the update process, I encountered an issue during the build where the following error message appeared. Error: Expected to find an ngsw-config.json configuration file in the /Users/nathanielmay/Code/firebaseTest folder. Either provide ...

Strategies for resolving a mix of different data types within a single parameter

Here, I am setting up the options params to accept a value that can either be a single string or another object like options?: string[] | IServiceDetail[] | IServiceAccordion[]; However, when attempting to map these objects, an error is encountered: Prope ...

Is there a way to set up custom rules in eslint and prettier to specifically exclude the usage of 'of =>' and 'returns =>' in the decorators of a resolver? Let's find out how to implement this

Overview I am currently working with NestJS and @nestjs/graphql, using default eslint and prettier settings. However, I encountered some issues when creating a graphql resolver. Challenge Prettier is showing the following error: Replace returns with (r ...