My package.json
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"#angular/common": "^2.1.1",
"#angular/compiler": "^2.1.1",
"#angular/core": "^2.1.1",
"#angular/forms": "^2.1.1",
"#angular/http": "^2.1.1",
"#angular/platform-browser": "^2.1.1",
"#angular/platform-browser-dynamic": "^2.1.1",
"#angular/router": "^3.1.1",
"#angular/upgrade": "^2.1.1",
"angular-in-memory-web-api": "^0.1.13",
"bootstrap": "^3.3.7",
"chart.js": "^2.4.0",
"core-js": "^2.4.1",
"ng2-charts": "^1.4.4",
"ng2-webstorage": "^1.5.0",
"reflect-metadata": "^0.1.8",
"rxjs": "^5.0.0-rc.4",
"systemjs": "0.19.39",
"zone.js": "^0.7.2"
},
"devDependencies": {
"#types/core-js": "^0.9.34",
"#types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
My app.module.ts
import { NgModule } from '#angular/core';
import {BrowserModule} from '#angular/platform-browser';
import {FormsModule, ReactiveFormsModule} from '#angular/forms';
import {HttpModule} from '#angular/http';
import {routing} from './app.routing';
import {ChartsModule} from 'ng2-charts/ng2-charts';
import {HttpHelper} from './shared/http-helper'
import {SpinnerComponent} from './misc/spinner/spinner.component';
import {PaginationComponent} from './misc/pagination/pagination.component';
import { AppComponent } from './app.component';
import {NavbarComponent} from './navbar/navbar.component';
import {AuthenticationComponent} from './components/authentication/authentication.component';
> import {HomeComponent} from
> './home/home.component'; import {HotelsComponent}
> from './components/hotel/hotels.component'; import {NewHotelComponent}
> from './components/hotel/new-hotel.component'
>
> import {AuthenticationService} from
> './components/authentication/authentication.service'; import
> {HotelsService} from
> './components/hotel/hotels.service';
>
> #NgModule({ imports: [
> BrowserModule
> , FormsModule
> , ReactiveFormsModule
> , HttpModule
> , routing
> , ChartsModule
> , Ng2Webstorage
> ], declarations: [
> AppComponent
> , NavbarComponent
> , AuthenticationComponent
> , HomeComponent
> , HotelsComponent
> , NewHotelComponent
> , SpinnerComponent
> , PaginationComponent
> ], providers :[
> HttpHelper
> , AuthenticationService
> , HotelsService], bootstrap: [ AppComponent ] }) export class AppModule { }
My systemjs.config.js
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
'ng2-charts' : 'npm:ng2-charts',
'underscore' : 'npm:underscore',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'ng2-charts':{
defaultExtension: 'js'
},
'underscore':{
defaultExtension: 'js'
}
}
});
})(this);
My application works fine with above code. Now I want add session storage to app so i used Ng2Webstorage (v1.5). I installed it using NPM. As soon as, I added following code:
Addition to app.module.ts
import {Ng2Webstorage} from 'ng2-webstorage';
...
...
#NgModule({
imports: [
...
,....
, Ng2Webstorage
],
...
})
Addition to systemjs.config.js
map:{
......
......
'ng2-webstorage' : 'npm:ng2-webstorage'
}
packages:{
....
....
,
'ng2-webstorage': {
main: 'bundles/core.umd.js',
defaultExtension: 'js'
}
}
I started receiving following error. I tried removing Ng2Webstorage and my app works fine.
"(SystemJS) ctorParameters.map is not a function TypeError:
ctorParameters.map is not a function
at ReflectionCapabilities.parameters (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:1780:49)
at Reflector.parameters (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:1922:48)
at CompileMetadataResolver.getDependenciesMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14534:56)
at CompileMetadataResolver.getTypeMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14499:28)
at eval (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14642:43)
at Array.forEach (native)
at CompileMetadataResolver.getProvidersMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14622:21)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14381:60)
at eval (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14324:52)
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:14311:46)
at RuntimeCompiler._compileComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:17063:49)
at RuntimeCompiler._compileModuleAndComponents (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:17001:39)
at RuntimeCompiler.compileModuleAsync (http://localhost:3000/node_modules/#angular/compiler/bundles/compiler.umd.js:16992:23)
at PlatformRef_._bootstrapModuleWithZone (http://localhost:3000/node_modules/#angular/core/bundles/core.umd.js:6684:29)
Evaluating http://localhost:3000/app/main.js Error loading
http://localhost:3000/app/main.js"
You can directly use setItem, getItem and clear methods of sessionStorage as below :
To set abdheajskke token value with key token in session storage use :
sessionStorage.setItem("token", "abdheajskke")
To get token value from session storage use :
sessionStorage.getItem("token")
Or you can create your own service to use sessionStorage. Below is the example of simple minimal service which you can use :
import { Injectable } from '#angular/core';
#Injectable()
export class StorageService {
public setItem(key: string, data: any) {
sessionStorage.setItem(key, JSON.stringify(data));
}
public getItem(key: string) {
if (!!sessionStorage.getItem(key)) {
return sessionStorage.getItem(JSON.parse(key));
} else {
return false;
}
}
public clear() {
sessionStorage.clear();
return true;
}
public removeItem(key: string) {
sessionStorage.removeItem(key);
return true;
}
}
Related
I'm pretty new to Angular, doing my first app. I wanted to use Angular Material library icons and encountered an error saying that 'mat-icon' is not a known element:.
Full error here
error NG8001: 'mat-icon' is not a known element:
1. If 'mat-icon' is an Angular component, then verify that it is part of this module.
2. If 'mat-icon' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
11 <mat-icon>favorite</mat-icon>
From what I understood, the problem comes when you don't import MatIconModule in App module.
However, I still get the same error even after importing it.
Here is what I have tried so far.
html file, obviously error refers to this component, where I use the icon
<div>
<div>
<button mat-fab disabled aria-label="Example icon button with a heart icon">
<mat-icon>favorite</mat-icon>
</button>
</div>
</div>
app.modules.ts
import { BrowserModule } from "#angular/platform-browser";
import { NgModule } from "#angular/core";
import { HttpClientModule } from "#angular/common/http";
import { MatIconModule } from "#angular/material/icon";
import { MatButtonModule } from "#angular/material/button";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from "#angular/platform-browser/animations";
import { MatToolbarModule } from "#angular/material/toolbar";
import { MatCardModule } from "#angular/material/card";
#NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatCardModule,
MatToolbarModule,
HttpClientModule,
MatIconModule,
MatButtonModule,
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
And Package.json
{
"name": "angular-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"#angular/animations": "~10.2.5",
"#angular/cdk": "~10.2.7",
"#angular/common": "~10.2.5",
"#angular/compiler": "~10.2.5",
"#angular/core": "~10.2.5",
"#angular/forms": "~10.2.5",
"#angular/material": "^10.0.0",
"#angular/material-moment-adapter": "^10.0.0",
"#angular/platform-browser": "~10.2.5",
"#angular/platform-browser-dynamic": "~10.2.5",
"#angular/router": "~10.2.5",
"hammerjs": "^2.0.8",
"rxjs": "~6.6.7",
"tslib": "^2.0.0",
"zone.js": "~0.10.2"
},
"devDependencies": {
"#angular-devkit/build-angular": "~0.1002.4",
"#angular/cli": "~10.2.4",
"#angular/compiler-cli": "~10.2.5",
"#angular/language-service": "~10.2.5",
"#types/node": "^12.11.1",
"#types/jasmine": "~3.3.8",
"#types/jasminewd2": "~2.0.3",
"codelyzer": "^5.1.2",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~7.0.0",
"tslint": "~6.1.0",
"typescript": "~4.0.8"
}
}
I also tried npm i, but didn't help either.
Let me know please, if I should include more files so that you can understand the problem better.
Any help will be appreciated.
Update
Now instead of importing Material components in app.module.ts I've created `material.module.ts
and now import looks like so.
import { BrowserModule } from "#angular/platform-browser";
import { NgModule } from "#angular/core";
import { HttpClientModule } from "#angular/common/http";
import { MaterialModule } from "./material/material.module";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";
import { BrowserAnimationsModule } from "#angular/platform-browser/animations";
#NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MaterialModule
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
material.module.ts
import { NgModule } from "#angular/core";
import { MatButtonModule } from "#angular/material/button";
import { MatIconModule } from "#angular/material/icon";
const MaterialComponents = [
MatButtonModule,
MatIconModule,
];
#NgModule({
imports: [MaterialComponents],
exports: [MaterialComponents],
providers: [
{
provide: MAT_MOMENT_DATE_ADAPTER_OPTIONS,
useValue: {
strict: true,
useUtc: true,
},
},
],
declarations: [],
})
export class MaterialModule {}
Unfortunately, even though it looks better but doesn't work either.
I feel like there is version mismatch.
Have you already add the font in the index.html
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#300;400;500&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
Here is an example
Simply import MatIconModule and add MatIconModule in your imports array in app.module.ts
I have not been able to find a solution by researching the error Error loading /ng2-bootstrap/alert.js as "ng2-bootstrap/alert" from /Scripts/app.modules.js.
I have tried these suggested solutions in System.config.js but they do not solve the problem:
map: {
1. 'ng2-bootstrap': 'node_modules/ng2-bootstrap' or
2. 'ng2-bootstrap': 'node_modules/ng2-bootstrap/bundles/ng2-bootstrap.umd.js'
}
packages: {
1. 'ng2-bootstrap': { defaultExtension: 'js' },
2. 'ng2-bootstrap': { format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
}
This is my **System.config.js** which I have read is likely the source of the issue.
(function (global) {
window.System.config({
paths: {
// paths serve as alias
'npm:': '/libs/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: '/Scripts',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler':
'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser':
'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic':
'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
'moment': 'node_modules/moment/moment.js',
'ng2-bootstrap': 'node_modules/ng2-bootstrap'
},
// packages tells the System loader how to load when no filename and/or
no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'ng2-bootstrap': { format: 'cjs', main:
'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
'moment': { main: 'moment.js', defaultExtension: 'js' }
}
});
var ngBootstrapPackageNames = [
'accordion',
'alert',
'bundles',
'buttons',
'carousel',
'collapse',
'dropdown',
'esm',
'modal',
'pagination',
'popover',
'progressbar',
'rating',
'tabset',
'timepicker',
'tooltip',
'typeahead',
'util'
];
function ngBootstrapPackIndex(pkgName) {
//packages['#ng-bootstrap/ng-bootstrap/' + pkgName] = { main:
'index.js', defaultExtension: 'js' };
}
ngBootstrapPackageNames.forEach(ngBootstrapPackIndex);
})(this);
This is my **app.module.ts** which has been implied but does not seem to help.
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { AppRoutingModule } from './app.routing';
import { AppComponent } from './app.component';
import { CollapsiblePanelComponent } from
'./bootstrap- components/collapsiblePanel.component';
import { InputGroupAddOnComponent } from
'./bootstrap-components/input-group-addon.component';
import { CreateRegimenComponent } from
'./create-regimen/create-regimen.component'
import { AlertComponent } from 'ng2-bootstrap/alert';
import { CollapseModule } from 'ng2-bootstrap/collapse';
import Approuting = require("./app.routing");
//import { NgbModule } from '#ng-bootstrap/ng-bootstrap';
#NgModule({
imports: [
CollapseModule,
AlertComponent,
FormsModule,
HttpModule,
// InMemoryWebApiModule.forRoot(InMemoryDataService),
AppRoutingModule,
BrowserModule],
declarations: [
AppComponent,
CollapsiblePanelComponent,
InputGroupAddOnComponent,
CreateRegimenComponent,
CollapseModule
],
bootstrap: [
AppComponent,
CollapsiblePanelComponent,
AlertComponent,
CollapseModule
]
})
export class AppModule { }
This is my Packages.json where I have read that "#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20", is the correct syntax. This does not help either.
{
"version": "1.0.0",
"name": "aspnet",
"private": true,
"scripts": {
"postinstall": "typings install",
"typings": "typings"
},
"dependencies": {
"#angular/common": "~2.1.0",
"#angular/compiler": "~2.1.0",
"#angular/core": "~2.1.0",
"#angular/forms": "~2.1.0",
"#angular/http": "~2.1.0",
"#angular/platform-browser": "~2.1.0",
"#angular/platform-browser-dynamic": "~2.1.0",
"#angular/router": "~3.1.0",
"#angular/upgrade": "~2.1.0",
"#ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.20",
"#types/core-js": "^0.9.35",
"#types/reflect-metadata": "0.0.5",
"angular-in-memory-web-api": "~0.1.5",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"jquery": "^3.1.1",
"moment": "^2.17.1",
"ng2-bootstrap": "^1.3.3",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"#types/jquery": "^2.0.40",
"gulp": "^3.9.1",
"gulp-clean": "^0.3.2",
"gulp-concat": "^2.6.0",
"gulp-tsc": "^1.2.0",
"gulp-typescript": "^2.13.6",
"path": "^0.12.7",
"typescript": "^2.0.3",
"typings": "^1.4.0",
"webpack": "^2.2.1"
}
}
Here is my node_modules directory
How do I get ng2_bootstrap to load without error 404
I finally figured out how to load ng2-bootstrap. For those out there trying to do this there are many many suggestions out there that simply don't work, at least as of February 27, 2017. So it is very frustrating for anybody trying.
This is what I somehow pieced together from the many suggestions I found. The most important suggestion being from ng2-bootstrap themselves. (Although ng2-bootstrap is not complete either).
System.js
(function (global) {
window.System.config({
paths: {
// paths serve as alias
'npm:': '/libs/',
'ng2-bootstrap/ng2-bootstrap': "node_modules/ng2-bootstrap/ng2-bootstrap"
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: '/Scripts',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic':
'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
// ng2-bootstrap
**'moment': 'node_modules/moment',
'ng2-bootstrap': 'node_modules/ng2-bootstrap'**
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular2-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
},
'ng2-bootstrap': { format: 'cjs', main: 'bundles/ng2-bootstrap.umd.js', defaultExtension: 'js' },
'moment': { main: 'moment.js', defaultExtension: 'js' }
}
});
})(this);
app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { Routes, RouterModule } from '#angular/router';
import { appComponents as AppComponents, appRoutes as AppRoutes } from './app.routing';
import { AppComponent } from './app.component';
import { **ButtonsModule, DatepickerModule, AlertModule** } from **'ng2-bootstrap';**
import Approuting = require("./app.routing");
#NgModule({
imports: [
FormsModule,
HttpModule,
// InMemoryWebApiModule.forRoot(InMemoryDataService),
RouterModule.forRoot(AppRoutes),
AlertModule.forRoot(),
DatepickerModule.forRoot(),
ButtonsModule.forRoot(),
BrowserModule],
declarations: [
AppComponent,
IndexComponent,
IndexPatientsComponent
],
providers: [],
bootstrap: [
AppComponent
]
})
export class AppModule { }
I looked through the forums, but couldn't find anything relevant.
I just started the tutorial 3 days ago, got to Step 7, and when I add the InMemoryWebApiModule to the mix (as detailed in the steps), I get this error:
All of my config files are directly from what was provided in the tutorial, but I'll provide them here anyway.
Any ideas as to what's going on?
Here is my package.json:
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/angular/angular.io/blob/master/LICENSE"
}
],
"dependencies": {
"#angular/common": "~2.1.1",
"#angular/compiler": "~2.1.1",
"#angular/core": "~2.1.1",
"#angular/forms": "~2.1.1",
"#angular/http": "~2.1.1",
"#angular/platform-browser": "~2.1.1",
"#angular/platform-browser-dynamic": "~2.1.1",
"#angular/router": "~3.1.1",
"#angular/upgrade": "~2.1.1",
"angular-in-memory-web-api": "~0.1.13",
"bootstrap": "^3.3.7",
"core-js": "^2.4.1",
"reflect-metadata": "^0.1.8",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.39",
"zone.js": "^0.6.25"
},
"devDependencies": {
"#types/core-js": "^0.9.34",
"#types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3"
}
}
Here is app.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';
import { AppRoutingModule } from './app-routing.module'
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';
import { DashboardComponent } from './dashboard.component';
#NgModule({
imports: [
AppRoutingModule,
BrowserModule,
FormsModule,
HttpModule,
InMemoryWebApiModule.forRoot(InMemoryDataService)
],
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent
],
providers: [ HeroService ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Here is systemjs.config.js:
/**
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
app: 'app',
// angular bundles
'#angular/core': 'npm:#angular/core/bundles/core.umd.js',
'#angular/common': 'npm:#angular/common/bundles/common.umd.js',
'#angular/compiler': 'npm:#angular/compiler/bundles/compiler.umd.js',
'#angular/platform-browser': 'npm:#angular/platform-browser/bundles/platform-browser.umd.js',
'#angular/platform-browser-dynamic': 'npm:#angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'#angular/http': 'npm:#angular/http/bundles/http.umd.js',
'#angular/router': 'npm:#angular/router/bundles/router.umd.js',
'#angular/forms': 'npm:#angular/forms/bundles/forms.umd.js',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
Take a look at the CHANGELOG. You need to use the umd bundle, and remove it from the packages
In systemjs.config.js you should change the mapping to:
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
then delete from packages:
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
I am going through the NG2 tutorial on here, but I've hit a stumbling block.
When I try and import my AppRoutingModule in my app.module.ts file I get a 'Cannot find module './app-routing.module' error. I have seen this post on here, amongst other solutions, but they all seem to relate to systemjs and not webpack.
Here is my package.json:
{
"name": "heroes",
"version": "0.0.0",
"license": "MIT",
"angular-cli": {},
"scripts": {
"start": "ng serve",
"lint": "tslint \"src/**/*.ts\"",
"test": "ng test",
"pree2e": "webdriver-manager update",
"e2e": "protractor"
},
"private": true,
"dependencies": {
"#angular/common": "2.0.0",
"#angular/compiler": "2.0.0",
"#angular/core": "2.0.0",
"#angular/forms": "2.0.0",
"#angular/http": "2.0.0",
"#angular/platform-browser": "2.0.0",
"#angular/platform-browser-dynamic": "2.0.0",
"#angular/router": "3.0.0",
"angular2-in-memory-web-api": "0.0.21",
"core-js": "^2.4.1",
"rxjs": "5.0.0-beta.12",
"ts-helpers": "^1.1.1",
"zone.js": "^0.6.23"
},
"devDependencies": {
"#types/jasmine": "^2.2.30",
"angular-cli": "1.0.0-beta.15",
"codelyzer": "~0.0.26",
"jasmine-core": "2.4.1",
"jasmine-spec-reporter": "2.5.0",
"karma": "1.2.0",
"karma-chrome-launcher": "^2.0.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"protractor": "4.0.5",
"ts-node": "1.2.1",
"tslint": "3.13.0",
"typescript": "2.0.2"
}
}
Here is my 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 { RouterModule } from '#angular/router';
// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
import { AppRoutingModule } from './app-routing.module'; //ERROR
...
#NgModule({
declarations: [
AppComponent,
HeroDetailComponent,
HeroesComponent,
DashboardComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
InMemoryWebApiModule.forRoot(InMemoryDataService), //ERROR: Cannot find name 'InMemoryDataServiceInMemoryDataService
RouterModule.forRoot([
{
path: '', redirectTo: '/dashboard', pathMatch: 'full'
},
{
path: 'dashboard', component: DashboardComponent
},
{
path: 'detail/:id', component: HeroDetailComponent
},
{
path: 'heroes', component: HeroesComponent
}
])
],
providers: [HeroService],
bootstrap: [AppComponent]
})
export class AppModule { }
Here is my specs (I am using angular CLI):
angular-cli: 1.0.0-beta.15
node: 4.2.6
os: linux x64 (ubuntu)
Not sure what else I can try? I guess I need to import the module somehow?
I did try and run:
npm install angular-route
But this led to:
├──UNMET PEER DEPENDENCY
#angular/common#2.0.0
├──UNMET PEER DEPENDENCY
#angular/core#2.0.0
├──UNMET PEER DEPENDENCY
#angular/platform-browser#2.0.0
└── angular-route#1.5.8
Anyone have any suggestions?
Checkout the live example with all the files
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'detail/:id', component: HeroDetailComponent },
{ path: 'heroes', component: HeroesComponent }
];
#NgModule({
imports: [ RouterModule.forRoot(routes) ],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
According to comments, it seems that you might be missing the app-routing file altogether. Perhaps you missed the piece describing that, or sometimes the documentation might be outdated. Please check the plunker showing a working, live example with the needed files.
Right before making a new angular project, the CLI prompts you to either include routing or not, like this:
Would you like to add Angular routing? Yes
Make sure you type in Y when it does so, or else your project won't be made with routing files.
Following the Angular 2 "Hero tour" tutorial, I encounter the following error:
Unhandled Promise rejection: Template parse errors:
Can't bind to 'ngModel' since it isn't a known property of 'input'. ("
<div>
<label>name: </label>
<input [ERROR ->][(ngModel)]="hero.name" placeholder="name">
</div>
"): AppComponent#6:13 ; Zone: <root> ; Task: Promise.then ; Value:
My package.json content is:
{
"name": "angular2-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"npm run tsc:w\" \"npm run lite\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"#angular/common": "2.0.0-rc.5",
"#angular/compiler": "2.0.0-rc.5",
"#angular/core": "2.0.0-rc.5",
"#angular/forms": "0.3.0",
"#angular/http": "2.0.0-rc.5",
"#angular/platform-browser": "2.0.0-rc.5",
"#angular/platform-browser-dynamic": "2.0.0-rc.5",
"#angular/router": "3.0.0-rc.1",
"#angular/router-deprecated": "2.0.0-rc.2",
"#angular/upgrade": "2.0.0-rc.5",
"systemjs": "0.19.27",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12",
"angular2": "^2.0.0-beta.17",
"angular2-in-memory-web-api": "0.0.15",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.0.0",
"lite-server": "^2.2.0",
"typescript": "^1.8.10",
"typings":"^1.0.4"
}
}
app.module.ts content:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { AppComponent } from './app.component';
#NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
And app.component content :
import { Component } from '#angular/core';
export class Hero {
id: number;
name: string;
}
#Component({
selector: 'my-app',
template: `
<h1>{{title}}</h1>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name">
</div>
`
})
export class AppComponent {
title = 'Tour of Heroes';
hero: Hero = {
id: 1,
name: 'Windstorm'
};
}
I spent time to read and apply solutions proposed in similar question on stackoverflow with no success.
What could be wrong?
Solution :
use
//main entry point
import {platformBrowserDynamic} from '#angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
instead of :
import { bootstrap } from '#angular/platform-browser-dynamic';
import { AppComponent } from './app.component';
bootstrap(AppComponent);
Your app.module.ts should look like this:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule, provideForms, disableDeprecatedForms } from '#angular/forms';
import { AppComponent } from './app.component';
#NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ],
providers: [
disableDeprecatedForms(),
provideForms()],
})
export class AppModule { }
UPDATE: this is now obsolete with RC6 and beyond
In RC6 it looks like this:
...
import { FormsModule } from '#angular/forms';
#NgModule({
imports: [ ..., FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
I.e. provideForms, disableDeprecatedForms are removed as deprecated. Check https://github.com/angular/angular/blob/master/CHANGELOG.md for details.
Thanks for it, unless that part...nothing going on!
import { FormsModule, provideForms, disableDeprecatedForms } from '#angular/forms';
A similar error occurs if you don't update the app.modules.ts file as required.
That is adding the following import
import { FormsModule } from '#angular/forms'; and the FormsModule as follows:
imports: [ BrowserModule, FormsModule ],
The app works fine. But I had this error in tests. You should import FormsModule in app.component.spec.ts like in app.module.ts to fix this.