Angular 2 service not able to resolve all parameters - json

Hi I am trying this for last two days using http reading local json file ,not sure where the problem is
my service file
import {Injectable} from '#angular/core';
import { Http,Response } from '#angular/http';
import { Observable } from 'rxjs/Observable'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/catch'
export class AcquirerService{
private _url : string = "agent_output1.json";
constructor(private _http : Http){}
getAcquirer(){
return this._http.get(this._url).subscribe(
(data) => console.log(data)
)
// .map((response:Response) => response.json());
}
my component file
import { Component ,OnInit} from '#angular/core';
import {AcquirerService} from '../services/acquirer.service'
#Component({
selector : 'acquirer-message',
templateUrl: '../templates/acquirer.component.html',
providers :[AcquirerService]
})
export class AcquirerComponent implements OnInit{
acquirer_response ;
constructor(private _acquirerService : AcquirerService){
}
ngOnInit(){
this._acquirerService.getAcquirer();
// .subscribe(resAcquirerData => this.acquirer_response = resAcquirerData);
}
}
The error which i am getting is
metadata_resolver.js:972 Uncaught
SyntaxError {__zone_symbol__error: Error: Can't resolve all parameters for AcquirerService: (?)
my main.ts file
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { enableProdMode } from '#angular/core';
import { environment } from './environments/environment';
import { AppModule } from './app/app.module';
import {HttpModule} from '#angular/http'
if (environment.production) {
enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);
my app module file
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule,JsonpModule } from '#angular/http';
import { AppComponent } from './components/app.component';
import {AcquirerComponent} from "./components/acquirer.component";
import {IssuerComponent} from "./components/issuer.component";
#NgModule({
declarations: [
AppComponent,AcquirerComponent,IssuerComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule
],
providers: [],
bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }
any help will be much appreciated I am new to angular

Add AcquirerService into providers
#NgModule({
declarations: [
AppComponent,AcquirerComponent,IssuerComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule
],
providers: [AcquirerService],
bootstrap: [AppComponent,HttpModule]
})
export class AppModule { }
plus add #Injectable() before AcquirerService
#Injectable()
export class AcquirerService{ ... }

In your service file, you imported the Injectable but didn't use it. Put #Injectable() before the service name like this
#Injectable()
export class AcquirerService{
//your code here
}

Related

Angular - Can't import a component from a library

I'm learning Angular and I decided to create separate generic elements in addition to the main project (in a library) and then import and use them according to my needs.
My problem is due to the fact that I can't import the generic components now... (I can import the Module from the library, but when I try to use my component's selector, Angular warns that the element is unknown to it and doesn't compile the server ).
Generic Component:
(.html)
<p>f-base-screen works!</p>
(.ts)
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'nx-flow-base-screen',
templateUrl: './f-base-screen.component.html',
styleUrls: ['./f-base-screen.component.scss'],
})
export class FBaseScreenComponent implements OnInit {
constructor() {}
ngOnInit(): void {}
}
Library Module:
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { FBaseScreenComponent } from './f-base-screen/f-base-screen.component';
#NgModule({
imports: [
CommonModule
],
declarations: [
FBaseScreenComponent
],
exports: [
FBaseScreenComponent
],
})
export class FLibrariesModule {}
f-libraries.ts (exports the module and its components):
export * from './f-libraries.module';
export * from './f-base-screen/f-base-screen.component';
My SharedModule (it's later imported into my app.module):
import { CommonModule } from "#angular/common";
import { NgModule } from "#angular/core";
import { FLibrariesModule } from "#nx-flow/f-libraries";
#NgModule({
imports: [CommonModule, FLibrariesModule],
exports: [CommonModule, FLibrariesModule],
providers: []
})
export class SharedModule {}
App.module.ts:
import { NgModule } from '#angular/core';
import { HttpClientModule } from '#angular/common/http';
import { BrowserModule } from '#angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { FRestService } from './shared/utils/rest.service';
import { SharedModule } from './shared/shared.module';
#NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, HttpClientModule, SharedModule],
exports: [],
providers: [FRestService],
bootstrap: [AppComponent],
})
export class AppModule {}
My Home.component.module.ts (which imports the SharedModule to use the nx-flow-base-screen component):
import { NgModule } from '#angular/core';
import { WelcomeService } from '../../shared/services/flow-services/welcome.service';
import { SharedModule } from '../../shared/shared.module';
import { FRestService } from '../../shared/utils/rest.service';
import { HomeComponent } from './home.component';
#NgModule({
declarations: [HomeComponent],
imports: [SharedModule],
providers: [WelcomeService, FRestService],
bootstrap: [HomeComponent],
})
export class HomeComponentModule {}
My Home.component.html (using the component, the error that appears in the image is the same as angular throws when I compile):
<div>
<span>Home Page</span>
</div>
<nx-flow-base-screen></nx-flow-base-screen>
I already researched and followed tutorials, none of them worked so far. Are there any changes I still need to make to be able to use my component, from the library, within my main project?
If any more files are needed to reach a conclusion, I'll post a print of it.
Error:
apps/flow/src/app/views/home/home.component.html:4:3 - error
NG8001: 'nx-flow-base-screen' is not a known element:
If 'nx-flow-base-screen' is an Angular component, then verify that it is part of this module.
If 'nx-flow-base-screen' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component
to suppress this message.

Angular - Scrolling bar error "TypeError: tView is null"

I would like to make a scroll widget for my array in html file but I get an error:
"ERROR ERROR: Not Caught (in promise): Error type: tView is null". I don't know why it doesn't work. To make scrollbar i use CdkScrollableModule, ScrollDispatcher services.
app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {RouterModule, Routes, Scroll} from "#angular/router";
import {NewPlanComponent} from "../new-plan/new-plan.component";
import {ManagementComponent} from "../management/management.component";
import {TeachersComponent} from "../management/teachers/teachers.component";
import {RoomsComponent} from "../management/rooms/rooms.component";
import {SubjectsComponent} from "../management/subjects/subjects.component";
import {GroupsApiService, RoomsApiService, SubjectsApiService, TeachersApiService} from "../api.services";
import {HttpClient, HttpClientModule} from "#angular/common/http";
import {enableProdMode} from '#angular/core';
import { TeacherComponent } from '../management/teachers/teacher/teacher.component';
import {FormsModule, ReactiveFormsModule} from '#angular/forms';
import { RoomComponent } from '../management/rooms/room/room.component';
import { SubjectComponent } from '../management/subjects/subject/subject.component';
import { GroupsComponent } from '../management/groups/groups.component';
import { GroupComponent } from '../management/groups/group/group.component';
import { NewTimetableComponent } from '../new-plan/new-timetable/new-timetable.component';
import {focusCellDirective} from '../shared/focus-cell.directive';
import {focusCellSubjectDirective} from "../shared/focus-cell-subject.directive";
import {ScrollDispatcher} from "#angular/cdk/overlay";
import {CdkScrollableModule, ScrollingModule} from "#angular/cdk/scrolling";
import {CdkVirtualScrollAppendOnlyExample} from "../scroll-bar/scroll-bar.component";
import {BrowserAnimationsModule} from "#angular/platform-browser/animations";
import {MatNativeDateModule} from '#angular/material/core';
export const router: Routes = [
{ path: 'newplan', component:NewPlanComponent},
{ path: 'management', component:ManagementComponent},
{ path: 'management/teachers', component:TeachersComponent},
{ path: 'management/rooms', component:RoomsComponent},
{ path: 'management/subjects', component:SubjectsComponent},
{ path: 'management/teachers/teacher', component:TeacherComponent},
{ path: 'management/rooms/room', component:RoomComponent},
{ path: 'management/subjects/subject', component:SubjectComponent},
{ path: 'management/groups', component:GroupsComponent},
{ path: 'management/groups/group', component:GroupComponent},
{ path: 'newplan/newtimetable', component:NewTimetableComponent},
{ path: 'newplan/scrollbar', component:CdkVirtualScrollAppendOnlyExample},
];
#NgModule({
declarations: [
AppComponent,
ManagementComponent,
NewPlanComponent,
TeachersComponent,
TeacherComponent,
RoomsComponent,
RoomComponent,
SubjectComponent,
SubjectsComponent,
GroupsComponent,
GroupComponent,
NewTimetableComponent,
focusCellDirective,
focusCellSubjectDirective,
CdkVirtualScrollAppendOnlyExample,
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
RouterModule.forRoot(router),
FormsModule,
ReactiveFormsModule,
ScrollingModule,
CdkScrollableModule,
BrowserAnimationsModule,
MatNativeDateModule,
],
providers: [TeachersApiService,HttpClient,HttpClientModule,RoomsApiService,SubjectsApiService,GroupsApiService,ScrollDispatcher],
bootstrap: [AppComponent]
})
export class AppModule { }
new-timetable.component.html :
<html>
<body>
<cdk-virtual-scroll-viewport appendOnly class="example-viewport"y>
<div *cdkVirtualFor="let item of arr">
{{item}}
</div>
</cdk-virtual-scroll-viewport>
</body>
</html>
new-timetable.component.ts :
import { Component, OnInit } from '#angular/core';
import {GroupsApiService} from "../../api.services";
import {Groups} from "../../models/groups.model";
import {SubDb} from "../../models/subDb.mobels";
#Component({
selector: 'app-new-timetable',
templateUrl: './new-timetable.component.html',
styleUrls: ['./new-timetable.component.css']
})
export class NewTimetableComponent implements OnInit {
constructor() { }
arr=['fafsa','fsfafs','gsgad','fafsa','fsfafs','gsgad','fafsa','fsfafs','gsgad','fafsa','fsfafs','gsgad','fafsa','fsfafs','gsgad']
ngOnInit(): void {
}
}

Angular 9: Cannot instantiate cyclic dependency

I am reusing a service for HTTP communication which worked fine under Angular 8, but throws the following error under Angular 9:
Error: Cannot instantiate cyclic dependency! HttpService
at throwCyclicDependencyError (core.js:8122)
at R3Injector.hydrate (core.js:17206)
at R3Injector.get (core.js:16960)
at NgModuleRef$1.get (core.js:36337)
at Object.get (core.js:33981)
at getOrCreateInjectable (core.js:5880)
at Module.ɵɵdirectiveInject (core.js:21115)
at NodeInjectorFactory.DashboardComponent_Factory [as factory] (dashboard.component.ts:9)
at getNodeInjectable (core.js:6025)
at instantiateRootComponent (core.js:12788)
at resolvePromise (zone-evergreen.js:798)
at resolvePromise (zone-evergreen.js:750)
at zone-evergreen.js:860
at ZoneDelegate.invokeTask (zone-evergreen.js:399)
at Object.onInvokeTask (core.js:41640)
at ZoneDelegate.invokeTask (zone-evergreen.js:398)
at Zone.runTask (zone-evergreen.js:167)
at drainMicroTaskQueue (zone-evergreen.js:569)
at ZoneTask.invokeTask [as invoke] (zone-evergreen.js:484)
at invokeTask (zone-evergreen.js:1621)
This is the code of the HTTP service:
import { HttpClient } from '#angular/common/http';
import { Injectable } from '#angular/core';
#Injectable()
export class HttpService {
url: string = 'http://localhost:8000/';
constructor(private httpClient: HttpClient) { }
public getTestCall() {
return this.httpClient.get(this.url + 'test');
}
}
This is one of the components using HTTP service:
import { Component, OnInit } from '#angular/core';
import { HttpService } from 'src/app/services/http.service';
#Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss']
})
export class DashboardComponent implements OnInit {
constructor(private http: HttpService) { }
ngOnInit(): void { }
}
This is my app.module.ts:
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HttpService } from './services/http.service';
import { environment } from 'src/environments/environment';
import { DashboardComponent } from './views/dashboard/dashboard.component';
import { MaterialsComponent } from './views/materials/materials.component';
import { OrdersComponent } from './views/orders/orders.component';
import { CustomersComponent } from './views/customers/customers.component';
import { ModelsComponent } from './views/models/models.component';
import { ProductsComponent } from './views/products/products.component';
import { HttpClientModule } from '#angular/common/http';
#NgModule({
declarations: [
AppComponent,
DashboardComponent,
MaterialsComponent,
OrdersComponent,
CustomersComponent,
ModelsComponent,
ProductsComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [
HttpService,
{ provide: 'BACKEND_API_URL', useValue: environment.backendApiUrl }
],
bootstrap: [AppComponent]
})
export class AppModule { }
The components are referred within the app.component.html using the [routerLink] directive and app-routing.module.ts
Thanks in advance!
I prefer declaring the HTTP Services as:
#Injectable({
providedIn: 'root',
}) and not providing them in any module.
That way the Service is accessible on the whole app from the beginning and probably you won't have error like this.
It would be better If you include your apps modules in the question.
If you have more than one modules:
The problem could be, that the HttpService is provided in more than one of your modules.
In that case:
Solution can be to create a new module: HttpServiceModule:
#NgModule({
providers: [ HttpService ]
})
export class HttpServiceModule {}
After that, you have to delete in all other modules: providers: [ ...HttpService... ]

Logout From website if user does not do any activity in our web from Last 15 minutes

how to Logout From website if user does not do any move in our web from 15 minutes?
In Your angular app
npm install --save #ng-idle/core #ng-idle/keepalive angular2-moment
Set up your application module
Open src/app/app.module.ts and import the Ng2IdleModule using
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { NgIdleKeepaliveModule } from '#ng-idle/keepalive'; // this includes the core NgIdleModule but includes keepalive providers for easy wireup
import { MomentModule } from 'angular2-moment'; // optional, provides moment-style pipes for date formatting
import { AppComponent } from './app.component';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
MomentModule,
NgIdleKeepaliveModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Extend your Main component
import { Component, OnInit } from '#angular/core';
import { Router, NavigationStart } from '#angular/router';
import { Idle, DEFAULT_INTERRUPTSOURCES } from '#ng-idle/core';
import { Keepalive } from '#ng-idle/keepalive';
#Component({
selector: 'app-theme',
templateUrl: './theme.component.html',
styleUrls: ['./theme.component.css']
})
export class AppComponent implements OnInit {
lastPing?: Date = null;
constructor(private route: Router, private idle: Idle, private keepalive: Keepalive) {
idle.setIdle(5);
idle.setTimeout(900);
idle.setInterrupts(DEFAULT_INTERRUPTSOURCES);
idle.onIdleEnd.subscribe(() => { });
idle.onTimeout.subscribe(() => {
this.logout();
});
idle.onIdleStart.subscribe(() => {
});
idle.onTimeoutWarning.subscribe((countdown) => {
});
keepalive.interval(5);
keepalive.onPing.subscribe(() => this.lastPing = new Date());
this.reset();
}
ngOnInit() {
}
reset() {
this.idle.watch();
}
logout() {
//--
// LogoutCode
//---
}
}

Can't I use a component defined in a file other than app.component.ts in HTML directly?

I am facing difficulty in using a component defined in a file named navigation.component.ts directly on HTML Page.
The same component works fine if I use it under template of a component defined on app.component.ts.
Contents of app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppComponent } from './app.component';
import { NavigationComponent} from './shared/navigation.component';
#NgModule({
imports: [BrowserModule],
declarations: [AppComponent, NavigationComponent],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Contents of navigation.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'navigation',
templateUrl: '/views/shared/navigation.html'
})
export class NavigationComponent {
userName: string = 'Anonymous';
}
Contents of app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'main-app',
template: '<navigation></navigation><h1>{{pageTitle}}</h1>'
})
export class AppComponent {
pageTitle: string = 'Portal 2.0';
}
Contents of index.html
<body>
<main-app></main-app>
</body>
The above works and renders menus on top but when I try to use <navigation> directly (given below) it doesn't render it, doesn't show any errors either.
<body>
<navigation></navigation>
</body>
Am I doing something wrong?
And the bigger question is how I go debugging issues like this?
Yes you can use web components. Add all the components that you want to load to entrycomponents.
Using createCustomElement you can create elements and use their selector anywhere.
import { BrowserModule } from '#angular/platform-browser';
import { NgModule, Injector } from '#angular/core';
import { createCustomElement } from '#angular/elements';
import { AppComponent } from './app.component';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
entryComponents: [AppComponent]
})
export class AppModule {
constructor(private injector: Injector) {
console.log('Elements is loaded: Activation');
this.registerComponent('metro-activation-loader', AppComponent);
}
public ngDoBootstrap(): void {
console.log('Elements is loaded: Activation ngDoBootstrap');
}
// tslint:disable-next-line:no-any
private registerComponent(name: string, component: any): void {
const injector = this.injector;
const customElement = createCustomElement(component, { injector });
customElements.define(name, customElement);
}
}