How to inject service in private constructor - angularjs

I have an existing code implementing singleton pattern by having private constructor and returning object instance returning the object -
export class SingletonFactory {
private static factoryInstance = new SingletonFactory();
private constructor() {
}
public static getInstance() {
return SingletonFactory.factoryInstance;
}
}
I need to inject a dependency into this factory. And I changed my code to the following -
#Inject('MyService')
export class SingletonFactory {
private static factoryInstance = new SingletonFactory();
private constructor(private myService : MyService) {
}
public static getInstance() {
return SingletonFactory.factoryInstance;
}
}
Please suggest, how I can inject the dependency at object creation in the constructor?

Inject into your component.
import { Router } from '#angular/router';
import { FormBuilder, FormGroup, Validators} from '#angular/forms';
import { Location } from '#angular/common';
constructor(public location: Location,public fb:FormBuilder,public global_service:GlobalService,public router: Router) {
}

Related

AngularJS, TypeScript, Component Architecture [$injector:unpr] Unknown provider

I have the following issue and don't know how to resolve it.
Usually, I work in Angular 6 but get back for a while to AngularJS, and have some issues with DI I think.
I have the component architecture with typescript.
My appRootModule looks like this (w/o imports section)
export const appRootModule: IModule = module('testApp, ['asyncFilter'])
.component('appRoot', new AppRootComponent())
.component('postModal', new PostModalComponent())
.service('componentsDataService', ComponentDataService);
And that's the service that gives the error.
appRootController:
export class AppRootController implements IController
private $http: IHttpService;
private $cds: ComponentDataService;
constructor($http: IHttpService, $cds: ComponentDataService) {
this.$http = $http;
this.$cds = $cds;
/.../
}
public sendToModal(post: any): void {
this.$cds.sendModalData(post);
}
public $onInit = async (): Promise<void> => {};
}
And the service file:
import {Observable} from 'rxjs/Observable';
import {Subject} from 'rxjs/Subject';
export class ComponentDataService {
private modalSubject: Subject<any> = new Subject<any>();
public sendModalData(post: any): void {
/.../
}
public clearModalData(): void {
/.../
}
public getModalData(): Observable<any> {
return this.modalSubject.asObservable();
}
}
Please help. I'm about to cry.
it gives me :
[$injector:unpr] Unknown provider: $cdsProvider <- $cds
When TypeScript transpiles your code to javascript it strips out the types and you end up with something like
var AppRootController = (function() {
function AppRootController($http, $cds) { /.../ }
});
AngularJS can't find $cds and you get the error you're seeing. You need to use the name you have assigned to your service: componentsDataService. i.e.:
export class AppRootController implements IController
private $http: IHttpService;
private $cds: ComponentDataService;
constructor($http: IHttpService, componentsDataService: ComponentDataService) {
this.$http = $http;
this.$cds = componentsDataService;
/.../
}
public sendToModal(post: any): void {
this.$cds.sendModalData(post);
}
public $onInit = async (): Promise<void> => {};
}

Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin

I am trying to create a Minecraft plugin with a command that will set the world name into config.yml. Except I keep getting "Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin" when I attempt to set the config. I have already searched around for several way to fix this but I have not understood have to implement other situations into mine.
Here is my code:
Main.java:
package me.Liam22840.MurderRun;
import org.bukkit.plugin.java.JavaPlugin;
import me.Liam22840.MurderRun.commands.HelpCommand;
import me.Liam22840.MurderRun.commands.SetmapCommand;
public class Main extends JavaPlugin {
#Override
public void onEnable(){
loadConfig();
new HelpCommand(this);
new SetmapCommand(this);
}
public void loadConfig(){
getConfig().options().copyDefaults(true);
saveConfig();
}
}
SetmapCommand.java:
package me.Liam22840.MurderRun.commands;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import Utils.Utils;
import me.Liam22840.MurderRun.Main;
import me.Liam22840.MurderRun.getConfig;
public class SetmapCommand implements CommandExecutor{
private int count;
public SetmapCommand(Main plugin){
plugin.getCommand("Setmap").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)){
sender.sendMessage("Only players can execute this command!");
return true;
}
Player p = (Player) sender;
Location b_loc = p.getLocation();
if(p.hasPermission("MurderRun.Setworld")){
Main.getConfig().set("Maps." + p.getName() + count + ".World", b_loc.getWorld().getName());
Main.saveConfig();
p.sendMessage(Utils.chat("&4Map Set"));
return true;
} else{
p.sendMessage("You do not have the required permissions to execute this command!");
}
return false;
}
}
You can't directly call the Main class, because it is not static. To call it, you should do this in your Setmap class and the constructor:
private Main plugin;
public SetmapCommand(Main plugin){
this.plugin = plugin;
plugin.getCommand("Setmap").setExecutor(this);
}
After you did this, you can use in your Setmap class:
plugin.saveConfig();

Create singleton class in typescript angular

I have a class with two method. I need to execute the first at the app bootstrap and the second few times during the application live.
My class:
import { Profile } from './Profile';
class Profilatura {
public profile: Profile;
/** #ngInject */
first($http: angular.IHttpService): void{
.......
this.profile = ...;
.......
}
public second(): Profile{
return this.profile;
}
}
In my app module:
import { ProfilaturaService } from './profilatura/ProfilaturaService';
angular.module('App')
.run(function runBlock($http: angular.IHttpService) {
Profilatura.first($http);
})
....
Why I get Property 'first' doesn't exist on type typeof?????
Profilatura is a class object. You need to make first and second methods static. Beware - when you do that, you cannot use this inside the static methods anymore:
class Profilatura {
public static profile: Profile;
/** #ngInject */
static first($http: angular.IHttpService): void{
.......
Profilatura.profile = ...;
.......
}
static public second(): Profile{
return this.profile;
}
}
You can also make Profilatura a singleton class by doing something like this:
class Profilatura {
private static instance: Profilatura;
static getInstance() {
if (!Profilatura.instance) {
Profilatura.instance = new Profilatura();
}
return Profilatura.instance;
}
public profile: Profile;
/** #ngInject */
first($http: angular.IHttpService): void{
.......
this.profile = ...;
.......
}
public second(): Profile{
return this.profile;
}
}
and then use it like:
Profilatura.getInstance().first(...)

In Angular+Typescript can I inject services and provide parameter in constructor

If I have a service injection like this:
export class MyService {
private fooService: IFooService;
public static $inject = [ "fooService" ];
constructor(fooService: FooService) {
this.fooService = fooService;
}
}
But I would like to give fooService a parameter in its constructor, so can I?
If you want to calculate something or provide the parameter in the run/config phase use a Provider
class MyServiceProvider implements ng.IServiceProvider {
private customParameter;
public setCustomParameter(val) {
this.customParameter = val;
}
public $get():Function {
return () => {
return new MyService(this.customParameter);
};
}
}
You now can inject the MyServiceProvider in the config phase and call the setCustomParameter.
If you know what what the parameter is, you can use regular injection and just define an angular variable like this:
angular.constant(CONSTANT_NAME, 'CONSTANT_VALUE');
export class fooService {
public static $inject = [ "CONSTANT_NAME" ];
constructor(CONSTANT_NAME) {
}
}

actionscript 3 - using array in multiple classes

So I have 2 files, where I want to be able to access an array from 1 file in the other.
package code {
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.ui.Keyboard;
import code.*;
public class Init extends MovieClip {
public var _solidObjects: Array;
public function Init() {
_solidObjects = [wall01, wall02, wall03, wall04];
}
}
}
How would I be able to access the _solidObjects array from another class in a seperate file?
Any help would be appreciated as I have been trying for a while with no success, thanks.
Constructors can be passed variables. For example:
First class:
package code {
public class Init extends MovieClip {
public var solidObjects: Array;
public function Init() {
solidObjects = [wall01, wall02, wall03, wall04];
}
}
Second class:
package code {
public class SomeClass extends MovieClip {
public var solidObjects: Array;
public function SomeClass(param:Array) {
this.solidObjects = param;
}
}
}
Usage context:
var initObj:Init = new Init();
var secondObject:SomeClass = new SomeClass(initObj.solidObjects);

Resources