Weird behavior with Angular Material datepicker - html

Problem Statement
I have implemented the Angular Material date picker but it doesn't allow for me to view it when I navigate to the page with it in there. Instead, it just redirects me back to the home page for some extremely strange reason even though this has nothing to do with routing!
Code
This is the code that redirects me to the home page whenever I try to view it. This does not work, even though it compiles:
<mat-form-field class="dobContain">
<input matInput [matDatepicker]="datePicker" class="field" maxlength="30" type="text"
placeholder="Date of Birth">
<mat-datepicker-toggle matSuffix [for]="datePicker"></mat-datepicker-toggle>
<mat-datepicker #datePicker></mat-datepicker>
</mat-form-field>
This is the code that works (the same input field but without the date picker):
<mat-form-field class="dobContain">
<input matInput class="field" maxlength="30" type="text" placeholder="Date of Birth">
</mat-form-field>
Explanation of Code
The first block of code blocks my access to view it. Whenever I use that piece of code and try to navigate to the page containing that code, it just redirects me back to the home page. Weird!
The second block of code works perfectly and is the same code, but it excludes the date picker. Nothing more to it.
Expected Results
I want to use the Angular Material datepicker for the date of birth field in the form.
Actual Results
The datepicker (for some reason) redirects me to the home page whenever I route to the page containing the datepicker code.
Addtional Information
My routing module
const routes: Routes = [
{ path: "home", pathMatch: "full", component: HomeComponent },
{ path: "products", pathMatch: "full", component: ProductsComponent },
{ path: "productX", pathMatch: "full", component: ProductXComponent },
{ path: "account", pathMatch: "full", component: AccountComponent },
{ path: "login", pathMatch: "full", component: AccountLoginComponent },
{ path: "signUp", pathMatch: "full", component: AccountSignUpComponent }, // The component with the datepicker
];
#NgModule({
imports: [RouterModule.forRoot(routes, { anchorScrolling: 'enabled' })
],
exports: [RouterModule]
})
export class AppRoutingModule { }
Imports
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { MatButtonModule } from '#angular/material/button';
import { MatDialogModule } from '#angular/material/dialog';
import { MatCardModule } from '#angular/material/card';
import { MatStepperModule } from '#angular/material/stepper';
import { MatInputModule } from '#angular/material/input';
import { MatDatepickerModule } from '#angular/material/datepicker';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { HomeComponent, GoldenCircleComponent } from './home/home.component';
import { ProductsComponent } from './products/products.component';
import { ProductXComponent } from './productX/productX.component';
import { AccountComponent } from './account/account.component';
import { AccountLoginComponent } from './account-login/account-login.component';
import { AccountSignUpComponent } from './account-sign-up/account-sign-up.component';
#NgModule({
declarations: [
AppComponent,
HomeComponent,
ProductsComponent,
ProductXComponent,
GoldenCircleComponent,
AccountComponent,
AccountLoginComponent,
AccountSignUpComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatButtonModule,
MatDialogModule,
MatCardModule,
MatStepperModule,
MatInputModule,
MatDatepickerModule
],
providers: [],
bootstrap: [AppComponent],
// Needed for mat dialog module
exports: [
GoldenCircleComponent
],
entryComponents: [
GoldenCircleComponent
]
})
export class AppModule { }
The component where I am navigating to the page with the datepicker.
<body>
<h1 class="title">Account</h1>
<button mat-raised-button id="login" class="accountBtns" routerLink="/login">Login</button>
<!-- The button that takes me to the datepicker page -->
<button mat-raised-button class="accountBtns" routerLink="/signUp">Sign Up</button>
</body>

After looking in my console, I found that there is an error message saying: ERROR Error: MatDatepicker: No provider found for DateAdapter. Then, after some research on Google, I found that you have to import the following:
import { MatDatepickerModule } from '#angular/material/datepicker';
import { MatNativeDateModule } from '#angular/material/core';
...
imports: [
...
MatDatepickerModule,
MatNativeDateModule
],
And place MatDatepickerModule in providers:
...
providers: [
MatDatepickerModule
],
...
This fixed my issue. I was stuck on this problem for 3 days because I didn't look in my console for an error. Now I know to look at my console whenever I have difficulties. Need to get in the habit of looking in my console...
Have a good day everyone!

Related

angular material calendar not showing properly

this is the html code i wrote
<mat-form-field appearance="fill">
<mat-label>Enter a date range</mat-label>
<mat-date-range-input [rangePicker]="picker">
<input matStartDate placeholder="Start date">
<input matEndDate placeholder="End date">
</mat-date-range-input>
<mat-hint>MM/DD/YYYY – MM/DD/YYYY</mat-hint>
<mat-datepicker-toggle matIconSuffix [for]="picker"></mat-datepicker-toggle>
<mat-date-range-picker #picker></mat-date-range-picker>
</mat-form-field>
styles.css
#import "~#angular/material/prebuilt-themes/deeppurple-amber.css";
app.module.ts code
note : i didnt show all the imports i wrote
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import {MatDatepickerModule} from '#angular/material/datepicker'
import {MatInputModule} from '#angular/material/input'
import {MatFormFieldModule} from '#angular/material/form-field'
import { MatNativeDateModule } from '#angular/material/core';
#NgModule({
declarations: [
AppComponent,
LoginComponent,
HeaderComponent,
FooterComponent,
LogoutComponent,
ActualteComponent,
ForumComponent,
AgendaComponent
],
imports: [
BrowserModule,
AppRoutingModule,
FormsModule,
HttpClientModule,
BrowserAnimationsModule,
MatDatepickerModule,
MatInputModule,
MatFormFieldModule,
MatNativeDateModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
this is the output
i used the code from the official angular material website and its like
this
I too had some issues when using the mat-date picker component from angular here's what i did
I used the same html code you posted
Added Angular material using ng add #angular/material
I had to import some more modules other than the date picker module as i encountered some errors. Here's my app.module.ts
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { MatDatepickerModule } from '#angular/material/datepicker';
import { MatFormFieldModule } from '#angular/material/form-field';
import { MatInputModule } from '#angular/material/input';
import { AppComponent } from './app.component';
import { BrowserModule } from '#angular/platform-browser';
import {MatNativeDateModule} from '#angular/material/core';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserAnimationsModule,
MatNativeDateModule,
BrowserModule,
CommonModule,
MatDatepickerModule,
MatFormFieldModule,
MatInputModule,
],
exports: [
MatDatepickerModule,
MatFormFieldModule,
MatInputModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
I restarted the app and ran it and it worked fine for me.
I didn't have to do the import for the deep purple theme as it showed the option to select one when adding the angular material.
My Angular version is Angular: 15.1.5 and the material version is "#angular/material": "^15.1.5",

'mat-toolbar' is not a known element

I used in app.component.html :
<mat-toolbar>
Responsive side navigation
</mat-toolbar>
and I define MatToolbarModule in app.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { AppRoutingModule } from './app-routing/app-routing.module';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { MatToolbarModule } from '#angular/material/toolbar';
import { AppComponent } from './app.component';
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
MatToolbarModule,
MatSidenavModule,
MatButtonModule,
MatIconModule,
MatDividerModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
I obtain this error when I execute ng build command:
Error: src/app/app.component.html:1:1 - error NG8001: 'mat-toolbar' is not a known element:
1. If 'mat-toolbar' is an Angular component, then verify that it is part of this module.
2. If 'mat-toolbar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
please, how to correct this error
Based on what you shared (before any update) your set up is sufficient to make it working. In case you are on VSCode closing and opening the IDE would be sufficient sometimes to get ride of this kind of compilation errors.
Here is a stackblitz compatible with your code above works fine.

Why I am rendering to main page when I work on current page using routing

I am using the following code in visual studio. Here is the routing part when I click to FAQ component through page component and perform according in FAQ component backs to page component.
Here is routing part
import { NgModule } from '#angular/core';
import { RouterModule, Routes } from '#angular/router';
import { PageComponent } from './components/page/page.component';
import { FaqComponent } from './Folder/faq/faq.component';
import { HomeComponent } from './Folder/home/home.component';
const routes: Routes = [
{path: 'login', component: HomeComponent },
{path: 'data', component: FaqComponent },
{ path: '', component: PageComponent },
];
#NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
FaqComponent=>PageComponent
data => /
<a routerLink="/">xxx</a>

Angular 7 : ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[MenuWidgetComponent -> TripsMenu]:

In angular 7, I'm facing following issue, where I need to inject my MenuWidgetComponent in home component, I have imported it in widget component and exported in via index.ts. But still I'm getting,
I googled it but, could not found any solution. Did I miss something in my code?
Angular 7 : ERROR Error: Uncaught (in promise): Error:
StaticInjectorError(AppModule)[MenuWidgetComponent -> TripsMenu]:
Above error, Please help.
Folder Structrue:
ApplicationWorkspace >
1. GlobalApp >
- App >
- app.module
- home Component
2. Libs >
- widget >
src >
- MenuWidgetComponent
- widget module
index.ts
index.ts
export * from './lib/widgets.module';
export * from './lib/menu-widget/menu-widget.component';
app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { NxModule } from '#nrwl/nx';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { NgbModule } from '#ng-bootstrap/ng-bootstrap';
import { WidgetsModule } from '#gp-angular-workspace/widgets';
#NgModule({
declarations: [AppComponent, HomeComponent],
imports: [
BrowserModule,
NgbModule.forRoot(),
NxModule.forRoot(),
AppRoutingModule,
WidgetsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
Home.html
<div class="home" background="assets/images">
<pfj-menu-widget></pfj-menu-widget>
</div>
widget.module.ts
import { NgModule } from '#angular/core';
import { CommonModule } from '#angular/common';
import { WidgetWrapperComponent } from './widget-wrapper/widget-wrapper.component';
#NgModule({
imports: [CommonModule],
declarations: [MenuWidgetComponent],
exports: [ModalWidgetComponent]
})
export class WidgetsModule {}
This should get you fixed up:
#NgModule({
declarations: [AppComponent, HomeComponent],
imports: [
BrowserModule,
NgbModule.forRoot(),
NxModule.forRoot(),
AppRoutingModule,
WidgetsModule
],
providers: [],
entryComponents: [
WidgetsModule
],
bootstrap: [AppComponent]
})
Anything you want to inject into another component must be defined as an entry component. It's the same as if you were to inject a component into a mat-dialog.
See: https://angular.io/guide/entry-components for more info.
I had missed to import TripsMenu in widgets module.
import { TripsMenu } from './menu-widget/trips-menu';
And it worked!

Prosemirror editor tool bar incomplete on Angular 6

I have integrated ProseMirror in my Angular 6 as a text editor using Directives as below:
import { BrowserModule } from '#angular/platform-browser';
import { NgModule, Injector } from '#angular/core';
import { ProsemirrorModule } from './prosemirror/prosemirror.module';
import { BrowserAnimationsModule } from '#angular/platform-browser/animations';
import { MatButtonModule, MatCardModule, MatInputModule } from '#angular/material';
#NgModule({
declarations: [
AppComponent,
MyElementComponent
],
imports: [
BrowserModule,
ProsemirrorModule,
BrowserAnimationsModule,
MatButtonModule,
MatCardModule,
MatInputModule
],
providers: [],
})
export class AppModule {
}
ngDoBootstrap() {
}
}
and used html as:
<div appEditor></div>
all are working well...but toolbar does not appear properly...all aligned one below other
No idea how to make it to appear properly. Any help please