Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Directives on Component element

I'm trying to workout if this is possible.

I am trying to place a directive on a component element

WrapperComponent.ts

@Component({
    selector: 'app-wrapper',
    template: '<div><app-component draggable></app-component></div>'
})
export class WrapperComponent implements OnInit {

    constructor() {

    }

    ngOnInit() {
      //Code Here
    }
}

AppComponent.ts

@Component({
    selector: 'app-component',
    template: '<div>Sample Text</div>'
})
export class AppComponent implements OnInit {

    constructor() {

    }

    ngOnInit() {
      //Code Here
    }
}

DraggableDirective.ts

@directive({
    selector: '[draggable]',
})
export class DraggableDirective implements OnInit {

    constructor(private _element: ElementRef, renderer: Renderer) {

    }

    ngOnInit() {
      //Code Here

    }
}

But when i do this I get this error

zone.js:355 Unhandled Promise rejection: Template parse errors:
Can't bind to 'Draggable' since it isn't a known property of 'app-component'.
1. If 'app-component' is an Angular component and it has 'draggable' input, then verify that it is part of this module.
2. If 'app-component' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.
 ("<div>
    <app-component [ardDraggable]="true"></app-component>
</div>")

Is there something I am missing to make the component recognize the attribute as a directive instead of an input for the component.

like image 251
Ardenexal Avatar asked Jan 27 '26 13:01

Ardenexal


1 Answers

Change your selector in AppComponent and make sure that you have added WrapperComponent, AppComponent and DraggableDirective to your ngModule declaration :

@Component({
  selector: 'app-component',
  template: '<div>Sample Text</div>'
})

//AppModule:

@NgModule({
  imports:      [ BrowserModule ],
  declarations: [ AppComponent, WrapperComponent, DraggableDirective ],
  bootstrap:    [ WrapperComponent ]
})
like image 171
ranakrunal9 Avatar answered Jan 30 '26 09:01

ranakrunal9



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!