12

Error
Template parse errors:
Can't bind to 'time-delta' since it isn't a known property of 'p'.

Solution in documentation
I have to add the Directive to declarations array. I've done this already.

Now my architecture: I have three Modules:

  • AppModule (root)
  • TimeModule (should be a helper module later with some directives)
  • AuthModule (Login, Sign up components and so on)

The AppModule:

@NgModule({
    imports: [
        TimeModule,
        BrowserModule,
        FormsModule,
        AuthModule,
        routing,
    ],
    declarations: [
        AppComponent
    ],
    providers: [
        appRoutingProviders
    ],
    bootstrap: [AppComponent]
})

AuthModule:

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        authRouting
    ],
    declarations: [
        AuthComponent,
        LoginComponent,
        SignupComponent,
        LogoutComponent
    ],
    providers: [
        AuthGuard,
        AuthService
    ],
    bootstrap: [ LoginComponent ]
})

TimeModule:

@NgModule({
    declarations: [
        ReadableDatePipe,
        TimeDeltaDirective
    ]
})

And now I am trying to use my TimeDeltaDirective in a view of LoginComponent. And I already tried to add the directive to the other declarations arrays as well, but this won't work either.

I am thankful for any support! Thanks

TimeDeltaDirective:

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

@Directive({ selector: '[time-delta]' })
export class TimeDeltaDirective {
    constructor(renderer: Renderer, el: ElementRef) {
        function getTimeDelta(date: Date) {
            var now = new Date();
            return (now.getTime() - date.getTime()) / 1000;
        }

        this.delta = getTimeDelta(new Date(this.date));
    }

    @Input('date') date: string;
    delta: number;
}

usage in LoginComponent (example):

@Component({
    selector: 'login',
    template: `
    <p date="2016-09-28 00:00:00" [time-delta]>{{delta}}</p>
  `
})
Pablo Lozano
  • 10,122
  • 2
  • 38
  • 59
CodeWarrior
  • 354
  • 2
  • 3
  • 10

1 Answers1

17

You need to export TimeDeltaDirective from your TimeModule and then import TimeModule in your AuthModule because your LoginComponent is decalred there, and that's where you want to use your directive. This way, TimeDeltaDirective will be available for use in LoginComponent, as well as in other declared components of AuthModule. So, it should be something like this:

AuthModule

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        authRouting,
        TimeModule
    ],
    declarations: [
        AuthComponent,
        LoginComponent,
        SignupComponent,
        LogoutComponent
    ],
    providers: [
        AuthGuard,
        AuthService
    ],
    bootstrap: [ LoginComponent ]
})

TimeModule

@NgModule({
    declarations: [
        ReadableDatePipe,
        TimeDeltaDirective
    ],
    exports: [
        TimeDeltaDirective
    ]
})
Stefan Svrkota
  • 48,787
  • 9
  • 98
  • 87
  • Thank you for your answer. Unfortunately this is not working. I added the `TimeDeltaDirective` to the exports array and added the same to the imports array of `AuthModule`. Still the same error. Any other idea? – CodeWarrior Sep 28 '16 at 21:15
  • @UeliKramer Did you add `TimeDeltaDirective` or `TimeModule` to `AuthModule` imports? – Stefan Svrkota Sep 28 '16 at 21:16
  • I've added `TimeModule` to imports. – CodeWarrior Sep 28 '16 at 21:19
  • @UeliKramer Then your imports/exports are all fine, maybe there's a problem with your directive, you should post it here. – Stefan Svrkota Sep 28 '16 at 21:20
  • Yes, I added to my post up above. – CodeWarrior Sep 28 '16 at 21:24
  • 1
    Do you have any advise for me? – CodeWarrior Sep 29 '16 at 20:46
  • @UeliKramer I can't find the error, it would be nice if you could reproduce this on plnkr. Here's quickstart, you can edit it to match your code: https://angular.io/resources/live-examples/quickstart/ts/plnkr.html – Stefan Svrkota Sep 29 '16 at 21:51
  • I know have another problem. in constructor() using console.log(this); console.log(this.date); returns in browser in TimeDeltaDirective object with the correct values and variables, if I output this.date it gives undefined. What is the reason for this? I am confused – CodeWarrior Sep 30 '16 at 16:10