Saturday, January 20, 2018
Invalid '@FunctionalInterface' annotation; Greeting is not a functional interface
Sunday, January 14, 2018
StaticInjectorError NullInjectorError: No provider for MyService
Hi Guys,
You might have faced an error like "StaticInjectorError NullInjectorError: No provider for MyService".
This error comes when you try to create a service and something goes wrong.
Let's see the full stack trace.
ERROR Error: StaticInjectorError[MyService]:
StaticInjectorError[MyService]:
NullInjectorError: No provider for MyService!
at _NullInjector.get (webpack-internal:///../../../core/esm5/core.js:1189)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveNgModuleDep (webpack-internal:///../../../core/esm5/core.js:11074)
at NgModuleRef_.get (webpack-internal:///../../../core/esm5/core.js:12306)
at resolveDep (webpack-internal:///../../../core/esm5/core.js:12804)
I have few tips which will help you Debug this.
You might have faced an error like "StaticInjectorError NullInjectorError: No provider for MyService".
This error comes when you try to create a service and something goes wrong.
Let's see the full stack trace.
ERROR Error: StaticInjectorError[MyService]:
StaticInjectorError[MyService]:
NullInjectorError: No provider for MyService!
at _NullInjector.get (webpack-internal:///../../../core/esm5/core.js:1189)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveToken (webpack-internal:///../../../core/esm5/core.js:1477)
at tryResolveToken (webpack-internal:///../../../core/esm5/core.js:1419)
at StaticInjector.get (webpack-internal:///../../../core/esm5/core.js:1290)
at resolveNgModuleDep (webpack-internal:///../../../core/esm5/core.js:11074)
at NgModuleRef_.get (webpack-internal:///../../../core/esm5/core.js:12306)
at resolveDep (webpack-internal:///../../../core/esm5/core.js:12804)
I have few tips which will help you Debug this.
Let's see the working code
import { Component, OnInit } from '@angular/core';
import { MyService } from './myapp.service';
@Component({
selector: 'app-question',
templateUrl: './myapp.component.html',
styleUrls: ['./myapp.component.css'],
providers:[MyService ]
})
export class MyComponent implements OnInit {
constructor(private myService:MyService ) { }
//constructor() { }
ngOnInit() {
this.data=this.myService.loadData();
console.log(data);
}
var data={};
}
import { MyService } from './myapp.service';
@Component({
selector: 'app-question',
templateUrl: './myapp.component.html',
styleUrls: ['./myapp.component.css'],
providers:[MyService ]
})
export class MyComponent implements OnInit {
constructor(private myService:MyService ) { }
//constructor() { }
ngOnInit() {
this.data=this.myService.loadData();
console.log(data);
}
var data={};
}
In the above code I have successfully created a service .You need to check if you have specified the service in providers.If you have not added ,add it in the provider attribute of @Component decorator .
Apart from this you need to add the service to app.module.ts
Apart from this you need to add the service to app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { MyService } from './myapp.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [MyService],
bootstrap: [AppComponent]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { MyService } from './myapp.service';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule
],
providers: [MyService],
bootstrap: [AppComponent]
})
export class AppModule { }
Wednesday, January 3, 2018
Friday, December 29, 2017
Uncaught Error: Encountered undefined provider
Hi Guys,
Here is how you can fix the issue "Uncaught Error: Encountered undefined provider" . Have a look at the stack trace below.
compiler.js:485
Uncaught Error: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.
at syntaxError (compiler.js:485)
at eval (compiler.js:15725)
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (compiler.js:15710)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:14994)
at CompileMetadataResolver._getEntryComponentMetadata (compiler.js:15812)
at eval (compiler.js:15293)
at Array.map (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15293)
at JitCompiler._loadModules (compiler.js:34226)
To find out which part of the code is throwing this error , first have a check in the component.ts file.
Check the value of provider attribute in the @Component decorator .
Here is how you can fix the issue "Uncaught Error: Encountered undefined provider" . Have a look at the stack trace below.
compiler.js:485
Uncaught Error: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.
at syntaxError (compiler.js:485)
at eval (compiler.js:15725)
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (compiler.js:15710)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:14994)
at CompileMetadataResolver._getEntryComponentMetadata (compiler.js:15812)
at eval (compiler.js:15293)
at Array.map (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (compiler.js:15293)
at JitCompiler._loadModules (compiler.js:34226)
To find out which part of the code is throwing this error , first have a check in the component.ts file.
Check the value of provider attribute in the @Component decorator .
Invalid providers for "AppComponent" - only instances of Provider and Type are allowed
Hi Guys,
You might have faced the issue "Invalid providers for "AppComponent" - only instances of Provider and Type are allowed" .
The full stack trace is as below:
Uncaught Error: Invalid providers for "AppComponent" - only instances of Provider and Type are allowed, got: [?./user.service.ts?]
at syntaxError (webpack-internal:///../../../compiler/esm5/compiler.js:706)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:15963)
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15931)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15215)
at CompileMetadataResolver._getEntryComponentMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:16033)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:15514)
at Array.map (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15514)
at JitCompiler._loadModules (webpack-internal:///../../../compiler/esm5/compiler.js:34447)
To find the root cause of the issue first check the providers attribute of @Component decorator in the component.ts file.
If the value is anything other than a valid Typescript type you will get the above error.
You might have faced the issue "Invalid providers for "AppComponent" - only instances of Provider and Type are allowed" .
The full stack trace is as below:
Uncaught Error: Invalid providers for "AppComponent" - only instances of Provider and Type are allowed, got: [?./user.service.ts?]
at syntaxError (webpack-internal:///../../../compiler/esm5/compiler.js:706)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:15963)
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15931)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15215)
at CompileMetadataResolver._getEntryComponentMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:16033)
at eval (webpack-internal:///../../../compiler/esm5/compiler.js:15514)
at Array.map (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (webpack-internal:///../../../compiler/esm5/compiler.js:15514)
at JitCompiler._loadModules (webpack-internal:///../../../compiler/esm5/compiler.js:34447)
To find the root cause of the issue first check the providers attribute of @Component decorator in the component.ts file.
If the value is anything other than a valid Typescript type you will get the above error.
Sunday, December 10, 2017
property binding in Angular 5
Hi Guys,
Let's understand what is property binding in angular 5.
We all know that an HTML element has various properties like id , name , value, etc. . Angular provides a way to bind to the property of HTML elements , called property binding .
There are multiple use cases that can be simplified using property binding like , if you have to change the value of a text field dynamically , or enable/disable a button. Let's see how can we do it.
First create an html file which will act as a component . Name this file as test.component.html , in this file have a text field . To bind a property we need to put the property in []. Here we are going to bind the value property , so I have placed value in square brackets . Since I am going to bind this property to a dynamic value , I will do so by assigning the property to a variable myValue.
You need to define myValue in the component file . So , create a typescript file and name it as test.component.ts . The content of the file should be as below.
Let's understand what is property binding in angular 5.
We all know that an HTML element has various properties like id , name , value, etc. . Angular provides a way to bind to the property of HTML elements , called property binding .
There are multiple use cases that can be simplified using property binding like , if you have to change the value of a text field dynamically , or enable/disable a button. Let's see how can we do it.
First create an html file which will act as a component . Name this file as test.component.html , in this file have a text field . To bind a property we need to put the property in []. Here we are going to bind the value property , so I have placed value in square brackets . Since I am going to bind this property to a dynamic value , I will do so by assigning the property to a variable myValue.
<input type="text" [value]="myValue">
You need to define myValue in the component file . So , create a typescript file and name it as test.component.ts . The content of the file should be as below.
import { Component } from '@angular/core';
@Component({
selector: 'app-test',
templateUrl: './test.component.html'
})
export class TestComponent {
myValue="test";
}
Finally update your index.html as
@Component({
selector: 'app-test',
templateUrl: './test.component.html'
})
export class TestComponent {
myValue="test";
}
<app-test></app-test>
That's all , verify the code by refreshing the localhost:4200 in chrome browser . You will see the output as
Wednesday, December 6, 2017
node_modules appears empty, you may need to run 'npm install'
Hi Guys,
You might face the error node_modules appears empty, you may need to run 'npm install' if you try to test an angular 2/4/5 application .
The most common cause of this error is missing node_modules folder in the directory from which you have called ng serve.
To solve this take the following steps.
1. Check , if you have called ng serve from the correct location ? to serve the application you need to navigate to the project folder and then call ng serve .
2. If you are in correct folder , check if you have a folder named node_modules in it.If not your project is incomplete . Create a new application using ng new and replace the src folder of new app with src folder of your existing app.
Important links
S O
You might face the error node_modules appears empty, you may need to run 'npm install' if you try to test an angular 2/4/5 application .
node_modules appears empty, you may need to run 'npm install'
This error occurs when you use the command ng serve.
C:\projects\my-app> ng serve
Note that this command is used to start a server , serving the angular application .The most common cause of this error is missing node_modules folder in the directory from which you have called ng serve.
To solve this take the following steps.
1. Check , if you have called ng serve from the correct location ? to serve the application you need to navigate to the project folder and then call ng serve .
2. If you are in correct folder , check if you have a folder named node_modules in it.If not your project is incomplete . Create a new application using ng new and replace the src folder of new app with src folder of your existing app.
C:\projects> ng new my-app-new
Copy the src folder in my-app to my-app-new
C:\projects> cd my-app-new
C:\projects\my-app-new> ng serve
3. If you have node_modules folder available and in your app folder then it might be corrupted. To solve this create a new application using ng new and replace the src folder of new app with src folder of your existing app.C:\projects\my-app-new> ng serve
Important links
S O
Subscribe to:
Posts (Atom)
-
Hi Guys , In this blog I will share information related to error : can't bind to 'formcontrol' since it isn't a known pr...
-
Hi Guys , This blog is about the error " Target container is not a DOM element. " The full error in the console log is ...
-
Error Blog This blog is about the error Error: Objects are not valid as a React child Problem Statement error on start of...