-1

I am not build a service from angular lib, getting error. here is my code and error. any help please? I am using angular 8

import { Injectable } from '@angular/core';

// @dynamic
@Injectable({
  providedIn: 'root'
})
export class SharedSortByColumnService {

    constructor(
        private data: any,
        private searchCriteria: any,
        private childName: string,
        private secondChildName: string,
        private thirdChildname: string) {  }

    columnSorter() {

    }

}

error:

Can't resolve all parameters for SharedSortByColumnService in C:/722333/xxx/services/shared-sort-by-column.service.ts: (?, ?, ?, ?, ?).

UPDATE

I tried this:

 constructor(
        public data?: any,
        public searchCriteria?: any,
        public childName?: string,
        public secondChildName?: string,
        public thirdChildname?: string) {  }

still not work.

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • Are you looking for something like [this](https://kendaleiv.com/typescript-constructor-assignment-public-and-private-keywords/)? – Mridul Feb 13 '20 at 06:50

1 Answers1

3

You can't magically resolve a whole bunch of strings and "any" data types using DI.

Angular is good at resolving classes that are known to be injectable - but how is it meant to differentiate between the different string params here?

Where are those values coming from? If they are config values that remain unchanged for the lifetime of the app, then you could set them in the constructor (not by attempted DI):

@Injectable({
  providedIn: 'root'
})
export class SharedSortByColumnService {

  constructor() {  
    // TODO: set config values here
    // this.data = ??
  }

  private data: any;
  private searchCriteria: any;
  private childName: string;
  private secondChildName: string;
  private thirdChildname: string;

  columnSorter() {
  }
}

Edit:

I think you're implying that these are actually function parameters.

In which case it's trivial to write a function that acceps these parameters:

@Injectable({
  providedIn: 'root'
})
export class SharedSortByColumnService {

  constructor() {
    // empty constructor, can be deleted
  }

  columnSorter(data: any, searchCriteria: any, childName: string,
    secondChildName: string, thirdChildname: string): data {
    // TODO: process data. 
    // No need  to maintain internal state in the service. 
    // All parameters relate to this single transaction / operation

    return data;
  }
}

The service should only maintain internal state where it makes sense to maintain it over the lifetime of the app.

If you want to sort two data sets in the same component, for example, then you call the service twice and are "transactional" actions - the function calls are just a case of data + criteria in, and data out.

Kurt Hamilton
  • 12,490
  • 1
  • 24
  • 40
  • when we ignore the constructor, how i keep my service as reusable? how to pass the different prams to class? from where the data, searchCriteria will be assigned? – 3gwebtrain Feb 13 '20 at 12:09
  • What do you mean? I did caveat my answer with " If they are config values that remain unchanged for the lifetime of the app". How the state changes is up to you. If you want it to be per method, then you add the relevant method parameters. If the service maintains some internal state then it updates the properties at the relevant moments. Can you please explain what you mean. – Kurt Hamilton Feb 13 '20 at 12:23
  • Here I am sending the value in parms: https://jsfiddle.net/tux35f1h/ if the params not there then how can i pass the value to constructor? – 3gwebtrain Feb 13 '20 at 12:27
  • 1
    By passing the arguments in as function parameters, not class constructors https://jsfiddle.net/10gnu3bq/ – Kurt Hamilton Feb 13 '20 at 12:30
  • You should pass service-level state in as constructor parameters, and method-level state in as function parameters: https://jsfiddle.net/4e6baovj/ – Kurt Hamilton Feb 13 '20 at 12:33