Can child directives in Angular 2 harness the power of parent providers?

I am facing an issue while trying to utilize a service as a provider for general use in a Directive rather than a Component. The problem arises when the service is not being received in the child Directive, despite my expectation to use it within the directive:

// CurrentTimeService.ts
import { Injectable } from '@angular/core'

@Injectable()
export class CurrentTimeService {
  dt: number;

  constructor() {
    this.dt = new Date().getTime();
  }


}

app.ts
//our root app component
import {Component} from '@angular/core'
import {CurrentTimeService} from "./CurrentTimeService"
import {SimpleDirective} from "./SimpleDirective"

@Component({
  selector: 'my-app',
  providers: [CurrentTimeService],
  template: `
    <div>
      <h2 mySimpleDirective>Hello {{name}}</h2>

    </div>
  `,
  directives: [SimpleDirective]
})
export class App {
  constructor() {
    this.name = 'Angular 2 (Release Candidate!)'
  }
}

SimpleDirective.ts
import {Directive} from '@angular/core';
import { CurrentTimeService  } from "./currentTimeService"

@Directive({
    selector: '[mySimpleDirective]'
})
export class SimpleDirective {
  constructor(private currentTimeService: CurrentTimeService) {


  }

}

You can view the complete program on Plunker by following this link: https://plnkr.co/edit/s0zUkI?p=preview

Thank you for your help in advance.

Answer №1

Your SimpleDirective.ts file contains an incorrect import statement. Update it as follows:

import { CurrentTimeService  } from "./currentTimeService"

Change it to:

import { CurrentTimeService  } from "./CurrentTimeService"

For a working example, check out this Plunker demo

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

What is the best way to change a string into JSON format within Angular?

Embarking on processing the string below: const json = '{"list":"[{"additionalInformation": {"source": "5f645d7d94-c6ktd"}, "alarmName": "data", "description": "Validation Error. Fetching info has been skipped.", "eventTime": "2020-01-27T14:42:44 ...

Can we use classlist for adding or removing in Angular 2?

One of the features in my project is a directive that allows drag and drop functionality for elements. While dragging an element, I am applying classes to both the dragged element and the ones it's being dragged over. This is how I currently handle it ...

Transforming Javascript into Typescript with node modules in Visual Studio 2015

After developing a JavaScript web app using npm and webpack, I successfully converted all the .js files to .ts using the PowerShell command found here. Now, I am looking to transition to a VS2015 TypeScript project but struggling to find guidance on how ...

Optimizing File Transfers and Streaming Using Next.js and CDN Integration

As I work on developing a download system for large files on my website using Next.js and hosting the files on a CDN, I face the challenge of downloading multiple files from the CDN, creating a zip archive, and sending it to the client. Currently, I have i ...

Steps for upgrading from Ionic framework v1.2.4 to Ionic framework v2.0.0-beta with npm

Currently, I am in the process of upgrading my Ionic framework from version 1.2.4 to the latest version 2.0.0-beta using the node package manager (npm). I stumbled upon a guide on the official documentation site that instructed me to execute this command ...

Embedding a component inside another layout component in Angular

I'm attempting to insert a component into another layout component using a service, but I can't seem to get it working. Here is my current layout structure: AppComponent | AppLayoutComponent | ------------------------------------- ...

Obtain the dimensions (width and height) of a collection of images using Angular and TypeScript

Currently, I am facing an issue with my image upload functionality. My goal is to retrieve the width and height of each image before uploading them. However, I've encountered a problem where my function only provides the dimensions for the first image ...

How can we fetch data from the server in Vue 2.0 before the component is actually mounted?

Can anyone help me with this question noted in the title? How can I prevent a component from mounting in <router-view> until it receives data from the server, or how can I fetch the data before the component is mounted in <router-view>? Here a ...

Get rid of the filter arrows in the top row of a styled SpreadJS spreadsheet

Exploring SpreadJS for the first time has been an interesting journey as I dive into understanding table styling. While trying to apply styles across the entire table, I noticed a peculiar issue that arises. Upon applying a theme, whether it be custom or ...

What is the right sequence for invoking services in Angular 4?

My project layout consists of: https://i.stack.imgur.com/VnI56.png The user data service function below returns the user ID: get uid(): BehaviorSubject<string> { const uidSubject: Subject<string> = new Subject<string>(); this.a ...

What is the best way to arrange the information in JSON in ascending order and display it in a table format?

I am working with a mat-table and have used GET to display my data. I now want to sort the data in ascending order based on the db-nr from my JSON. Here is an excerpt from my JSON: { "period": 12.0, " ...

The filter becomes ineffective once I remove the input value

Check out this HTML table containing an input field that filters plans. https://i.stack.imgur.com/UfIw2.png I input the value => 1 The filter successfully works https://i.stack.imgur.com/CsQXh.png Removing the value (1) displays all recordings, tot ...

Encountering difficulty when trying to initiate a new project using the Nest CLI

Currently, I am using a tutorial from here to assist me in creating a Nest project. To start off, I have successfully installed the Nest CLI by executing this command: npm i -g @nestjs/cli https://i.stack.imgur.com/3aVd1.png To confirm the installation, ...

Issue with dependency injection in Angular 16 when used as a standalone feature

Here are my standalone API calls within the service: import { Injectable} from '@angular/core'; import { ProductEndPoints } from '../../constants/apiConstants/product-endpoints'; import { HttpClient} from '@angular/common/http&ap ...

Managing null values in RxJS map function

I'm facing a scenario where my Angular service retrieves values from an HTTP GET request and maps them to an Observable object of a specific type. Sometimes, one of the properties has a string value, while other times it's null, which I want to d ...

I am facing an issue with my ngmodel as it is not refreshing and the binding seems to

Currently, I am working with Ionic code and processing an image obtained through photo capture. While everything seems to be functioning correctly and I receive the blob data, the image is not displaying in the template. It appears that the Angular binding ...

We could not locate the export in Typescript, and the JSX element type does not show any construct or call signatures

Looking to utilize my Typescript React Component Library as a Package. The structure of my files is as follows: MyComponent.tsx: import React, { FunctionComponent } from 'react'; import styled from 'styled-components'; export interf ...

The NX monorepo from @nrwl is unable to locate the .svgr configuration file within the lib directory

Recently, I started working with NX Monorepo that consists of 2 separate react applications. In order to share icons between these apps, I decided to create an icons library. I made changes to the project.json file of the icons library and added a svg com ...

Revamping an npm package on GitHub

Currently, I am managing a project that has gained popularity among users and has received contributions from multiple individuals. The next step I want to take is to convert the entire library into TypeScript, but I am unsure of the best approach to ach ...

Property does not exist when dispatching in React Redux within componentDidMount

Currently, I am navigating my way through my initial project using React + Redux and have hit a few roadblocks while attempting to dispatch a function in the componentDidMount section. I tried to emulate the Reddit API example project from the Redux docume ...