垫徽章显示在按钮内部,而不是右上方

时间:2018-08-24 17:21:48

标签: angular angular-material badge

我正在尝试使用有角材料制成的徽章。我有一堆导航按钮要放在它们上,但是只是将数字放在按钮内,而不是像上面的示例那样在按钮的右上角放一个小圆圈,里面有数字:{{ 3}}

这是我的样子: https://material.angular.io/components/badge/examples

HTML:

<div fxLayout="column" fxLayoutAlign="start stretch">
  <button mat-raised-button type="button" matBadge="8" *ngFor="let item of items" [color]="item.Active ? 'accent' : null" fxLayout="row" fxLayoutAlign="space-between center" (click)="HandleItemClick(item)">
    <mat-icon *ngIf="item.MenuIcon">{{item.MenuIcon}}</mat-icon>
    <span>{{item.MenuText}}</span>
  </button>
</div>

CSS:

:host {
    display: block;
    background-color: #e0e0e0;
    height: 100%;
}

button {
    margin: 5px;
}

button mat-icon {
    margin-right: 10px;
}

div {
    background-color: #e0e0e0;
}

我尝试了所有选项。没有多少,他们什么也没改变。我确实将MatBadgeModule导入到我的app.material.module.ts文件中。这是我所有其他材料模块所在的位置,它们工作正常。我所做的与示例完全相同,但无法正常工作。

如果是依赖性问题,这是我的package.json:

{
  "name": "PURE",
  "version": "1.0.0",
  "scripts": {
    "ng": "ng",
    "start": "webpack-dev-server --port=4201",
    "start.vs2017": "webpack-dev-server --port=4201 --config ./webpack.vs.config.js",
    "start.aot.vs2017": "webpack-dev-server --port=4201 --aot --config ./webpack.vs.config.js",
    "build": "webpack",
    "build.aot": "webpack --config ./webpack.aot.config.js",
    "build.vs2017": "webpack --config ./webpack.vs.config.js",
    "build.aot.vs2017": "webpack --config ./webpack.vsaot.config.js",
    "test": "karma start ./karma.conf.js",
    "lint": "ng lint",
    "e2e": "protractor ./protractor.conf.js",
    "pree2e": "webdriver-manager update --standalone false --gecko false --quiet"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "6.0.0",
    "@angular/common": "6.0.0",
    "@angular/compiler": "6.0.0",
    "@angular/core": "6.0.0",
    "@angular/forms": "6.0.0",
    "@angular/http": "6.0.0",
    "@angular/router": "6.0.0",
    "@angular/platform-browser": "6.0.0",
    "@angular/platform-browser-dynamic": "6.0.0",
    "@angular/flex-layout": "6.0.0-beta.17",
    "@angular/material": "6.0.0",
    "@angular/cdk": "6.0.0",
    "core-js": "^2.4.1",
    "file-saver": "1.3.3",
    "moment": "2.22.2",
    "ng2-webstorage": "2.0.0",
    "rxjs": "6.0.0",
    "web-animations-js": "2.2.5",
    "zone.js": "^0.8.12"
  },
  "devDependencies": {
    "@angular/cli": "1.6.5",
    "@angular/compiler-cli": "5.2.5",
    "@angular/language-service": "5.1.2",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "autoprefixer": "^6.5.3",
    "codelyzer": "4.1.0",
    "css-loader": "^0.28.1",
    "cssnano": "^3.10.0",
    "exports-loader": "^0.6.3",
    "file-loader": "1.1.6",
    "istanbul-instrumenter-loader": "3.0.0",
    "jasmine-core": "2.99.1",
    "jasmine-spec-reporter": "4.2.1",
    "json-loader": "^0.5.4",
    "karma": "2.0.0",
    "karma-chrome-launcher": "2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "less-loader": "^4.0.2",
    "postcss-loader": "^1.3.3",
    "postcss-url": "^5.1.2",
    "protractor": "~5.1.2",
    "raw-loader": "^0.5.1",
    "sass-loader": "^6.0.3",
    "script-loader": "^0.7.0",
    "source-map-loader": "^0.2.0",
    "string-replace-webpack-plugin": "0.1.3",
    "style-loader": "^0.13.1",
    "stylus-loader": "^3.0.1",
    "ts-node": "3.2.0",
    "tslint": "5.9.1",
    "typescript": "2.5.3",
    "url-loader": "0.6.2",
    "webpack": "3.11.0",
    "webpack-dev-server": "2.11.1"
  },
  "-vs-binding": {
    "ProjectOpened": [
      "start.vs2017"
    ]
  }
}

1 个答案:

答案 0 :(得分:0)

根据您的代码检查此StackBlitz:Angular Material badge

HTML文件:

<div fxLayout="column" fxLayoutAlign="start stretch">
  <button 
    mat-raised-button 
    type="button" 
    matBadge="8" 
    *ngFor="let item of items" 
    [color]="item.Active ? 'accent' : null" 
    fxLayout="row" 
    fxLayoutAlign="space-between center"
    (click)="doSomething()">

    <mat-icon *ngIf="item.MenuIcon">{{item.MenuIcon}}</mat-icon>
    <span>{{item.MenuText}}</span>
  </button>
</div>

TS文件:

export class AppComponent  {
  name = 'Angular';

  items = [];

  constructor() {
    this.items = [
      { Active: true, MenuText: "Text 1", MenuIcon: "home" }
    ];
  }

  doSomething() {
    console.log("LOG");
  }
}

CSS文件:

:host {
    display: block;
    background-color: #e0e0e0;
    height: 100%;
}

button {
    margin: 5px;
}

button mat-icon {
    margin-right: 10px;
}

div {
    background-color: #e0e0e0;
}

app.module.ts文件:

import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {MatButtonModule} from '@angular/material/button';
import {MatIconModule} from '@angular/material/icon';
import {MatBadgeModule} from '@angular/material/badge';
import {FormsModule} from '@angular/forms';


  imports: [ 
      BrowserModule, FormsModule, BrowserAnimationsModule, 
      MatButtonModule, MatIconModule, MatBadgeModule ]

style.css文件:

@import "~@angular/material/prebuilt-themes/indigo-pink.css";

我认为这是您所缺少的,在棱角材料中添加了一种样式(这是必需的)。