当前位置: 首页 > 知识库问答 >
问题:

在AngularJs/Angular混合中不应用mat-form-field matInput默认样式

漆雕成弘
2023-03-14

我有一个AngularJs/Angular12.0。5个混合应用程序,我在向上移动到适当的角度。

在mat表单字段中使用具有matInput属性的输入时,它似乎只显示占位符/标签值,而不应用任何其他样式。如图所示:

不确定是什么原因导致了这种情况,因为角度材质模块已正确导入,并且显示这些字段时没有控制台错误。下面是我的代码:

应用程序模块

import {BrowserModule} from '@angular/platform-browser';
import {UpgradeModule} from '@angular/upgrade/static';
import {HttpClientModule} from "@angular/common/http";
import moduleName from "./angularJS.module";
import {UIRouterUpgradeModule} from "@uirouter/angular-hybrid";
import {NgModule} from "@angular/core";
import {UserProfileModule} from "./app/modules/user/user-profile.module";

@NgModule({
    imports: [
        BrowserModule,
        HttpClientModule,
        UpgradeModule,
        UserProfileModule,
        UIRouterUpgradeModule.forRoot()
    ]
})

export class AppModule {
    constructor(private upgrade: UpgradeModule) {
        console.log("Bootstrapping in Hybrid mode with Angular & AngularJS");
        upgrade.bootstrap(document.body, [moduleName], {strictDi: true});
    }

    ngDoBootstrap() {
    }
}

共享模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatButtonModule } from '@angular/material/button';
import { MatTableModule } from '@angular/material/table';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';
import { FlexLayoutModule } from '@angular/flex-layout';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

@NgModule({
    imports: [
        CommonModule
    ],
    exports: [
        BrowserAnimationsModule,
        FlexLayoutModule,
        FormsModule,
        MatToolbarModule,
        MatFormFieldModule,
        MatInputModule,
        MatSelectModule,
        MatButtonModule,
        MatTableModule,
        MatSlideToggleModule,
        ReactiveFormsModule
    ]
})
export class SharedModule { }

用户配置文件模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { UserProfileComponent } from './user-profile.component';

import { SharedModule } from "../../shared/shared.module";

@NgModule({
    declarations: [
        UserProfileComponent
    ],
    imports: [
        CommonModule,
        SharedModule
    ],
    exports: [
        UserProfileComponent
    ],
    entryComponents: [
        UserProfileComponent
    ]
})
export class UserProfileModule { }

用户简介组件

import {Component} from "@angular/core";
import {downgradeComponent} from "@angular/upgrade/static";
import angular from "angular";

@Component({
    selector: "user-profile",
    template: require("./user-profile.html")
})
export class UserProfileComponent {
    constructor() { }
}

angular
    .module('app')
    .directive("userProfile", downgradeComponent({
        component: UserProfileComponent
    }) as angular.IDirectiveFactory);

用户配置文件HTML

<div fxFill fxLayout="column" [class.mat-elevation-z1]="true">
    <mat-toolbar>
        <div class="profile-header">
            <h4>User Profile</h4>
        </div>
    </mat-toolbar>
    <form fxLayout="column">
        <mat-form-field>
            <input matInput placeholder="First Name">
        </mat-form-field>
        <mat-form-field>
            <input matInput placeholder="Last Name">
        </mat-form-field>
        <mat-form-field appearance="legacy">
            <mat-label>Standard form field</mat-label>
            <input matInput placeholder="Placeholder">
            <mat-hint>Hint</mat-hint>
        </mat-form-field>
    </form>
</div>

共有2个答案

卢宜然
2023-03-14

我通过执行以下操作修复了此问题

    打开angular.json
  1. 导航到项目
"styles": [
  "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
  "src/styles.scss"
],
常献
2023-03-14

原来我错过了Angular为其元素样式所需的默认主题。

我添加了一个材料主题。scss文件在我的根目录中,并在main中链接它。ts立即解决了问题。

材料主题

// Import library functions for theme creation.
@import '~@angular/material/theming';

// Include non-theme styles for core.
@include mat-core();

// Define your application's custom theme.
$primary: mat-palette($mat-indigo);
$accent:  mat-palette($mat-pink, A200, A100, A400);
$theme: mat-light-theme($primary, $accent);

// Include theme styles for Angular Material components.
@include angular-material-theme($theme);

主要TS

import'/材料主题。scss'

 类似资料:
  • 但是,对于我从Angular<5迁移的exsting项目,或者如果您在项目创建过程中忘记这样做,您如何更改这一点? 这个问题的意思是严格地与Angular版本5到6的突破性变化有关。

  • 混合应用 It's possible to connect infinite count of microservices to your existing Nest web application. 可能会在你现有的Nest web应用程序中上连接无数微服务。 Example: 示例: const app = NestFactory.create(ApplicationModule); cons

  • ●进入编辑地图页面—>点击打开图层管理窗口—>点样式,选择渲染方式,点“确定”保存设置。 ●普通点渲染:适用于标注数量小于2000,显示风格多样化的标注渲染,支持分类显示、自定义图标、标签设置等。

  • 我遇到了与使选定的mat工作相关的树问题(请不要让我问3个问题,因为它们都是相关的) 在单击空白区域之前,控件不可见 忽略按钮下方的红色消息只是一个提醒 单击空白区域时,将显示选择选项。 选择后,控件处于无效模式 没有任何控制台/角度误差 这里是组件 这是HTML 我在第一期中发现了另一个问题,但没有给出答案。 Matt-Select直到我点击它才显示 我还尝试了许多其他类似的答案,但没有成功(将

  • 我正在尝试用自定义样式覆盖ng2-bootstrap手风琴css样式,但该样式没有得到应用 HTML null null styles.css null null index.html null null Angular-CLI.json null null 我正在尝试更改背景颜色,但样式没有得到应用。有人能告诉我如何覆盖ng2-bootstrap手风琴的默认样式吗

  • 我已经在我的angularjs应用程序中实现了默认搜索,代码如下: 我在这里面临的问题是,假设有人碰巧触发了某个关键字,而这些关键字在被重复的记录中并不存在。我想要一条消息,说“什么也没找到”或者别的什么。 我如何实现这个逻辑?我在这里看了不同的文章和几个问题,找不到这方面的任何东西。我如何查看搜索的术语的长度是否为零,以便我可以ng-如果那个东西并显示消息?谢谢!