typescript - Is it possible to use parent provider in child directives in angular 2 -
i'm trying use service provider general use in directive instead component. complains not getting service in child directive. expected use service in directive:
// currenttimeservice.ts import { injectable } '@angular/core' @injectable() export class currenttimeservice { dt: number; constructor() { this.dt = new date().gettime(); } } app.ts //our root app component import {component} '@angular/core' import {currenttimeservice} "./currenttimeservice" import {simpledirective} "./simpledirective" @component({ selector: 'my-app', providers: [currenttimeservice], template: ` <div> <h2 mysimpledirective>hello {{name}}</h2> </div> `, directives: [simpledirective] }) export class app { constructor() { this.name = 'angular2 (release candidate!)' } } simpledirective.ts import {directive} '@angular/core'; import { currenttimeservice } "./currenttimeservice" @directive({ selector: '[mysimpledirective]' }) export class simpledirective { constructor(private currenttimeservice: currenttimeservice) { } /* uncomment command, working because doesn't demand service constructor() { } */ }
you check plunker out complete program: https://plnkr.co/edit/s0zuki?p=preview
thanks in advance
you have wrong import
statement in simpledirective.ts. change this
import { currenttimeservice } "./currenttimeservice"
to
import { currenttimeservice } "./currenttimeservice"
Comments
Post a Comment