【问题标题】:Angular8: Why don't I get space between these Angular Material cards?Angular8:为什么我没有在这些 Angular Material 卡片之间留出空间?
【发布时间】:2019-10-30 13:19:56
【问题描述】:

我正在学习 Angular 8,现在必须问我做错了什么,因为这些卡片之间应该有空间,下图显示了一条红线,应该有一些空间。

这是我的组件 html

<mat-card class="mat-space-bottom">
  <mat-card-header><mat-card-title>Book Search Api</mat-card-title></mat-card-header>
  <form [formGroup]="newContact" class="form-container">

    <mat-form-field>
      <input type="text" matInput placeholder="Title"  formControlName="title" #box (keyup.enter)="getTitle(box.value)"> <p>{{value}}</p>
    </mat-form-field>

    <mat-form-field>
      <input type="text" matInput placeholder="Author" formControlName="author" #box (keyup.enter)="getAutor(box.value)"> <p>{{value}}</p>
    </mat-form-field>

    <mat-form-field>
      <input type="text" matInput placeholder="Genre" formControlName="genre" #box (keyup.enter)="getGenre(box.value)"> <p>{{value}}</p>
    </mat-form-field>

    <mat-form-field>
      <input type="text" matInput placeholder="Price" formControlName="price" #box (keyup.enter)="getPrice(box.value)"> <p>{{value}}</p>
    </mat-form-field>

  </form>

</mat-card>


<mat-card class="mat-space-bottom" *ngFor="let phone of bookItems">

  <mat-card-header >
    <!-- <div mat-card-avatar class="firstLetter">{{phone.title | slice:0:1 | uppercase}}</div> -->
    <mat-card-title>{{phone.title | titlecase}}</mat-card-title>
    <mat-card-subtitle>{{phone.description}}</mat-card-subtitle>
    <mat-card-subtitle>{{phone.author}}</mat-card-subtitle>
    <mat-card-subtitle>{{phone.genre}}</mat-card-subtitle>
    <mat-card-subtitle>{{phone.publish_date}}</mat-card-subtitle>
    <mat-card-subtitle>{{phone.price}}</mat-card-subtitle>

  </mat-card-header>
  <!--
  <mat-card-actions>
    <a title="Edit Contact" routerLink="/contact/{{phone.id}}">
      <i class="material-icons">edit</i>
    </a>
    <a class="delete" title="Delete Contact" (click)="delete(phone, phone.id)">
      <i class="material-icons">close</i>
    </a>
  </mat-card-actions>
  </div> -->
</mat-card>

这是我的scss

mat-card {
  display: flex;
}

mat-card-header {
  width: 100%;
  display: flex;
  align-items: center;
}

mat-card-actions {
  margin-left: auto;
}

mat-card-subtitle {
  margin-bottom: 0;
}

mat-card:not(:last-child) {
  margin-bottom: 15px;
}

mat-card-actions {
  padding-top: 0;
}

.firstLetter {
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 22px;
}

mat-card:nth-child(1n) .firstLetter {
  background: #2e6da4;
}

mat-card:nth-child(2n) .firstLetter {
  background: #4cae4c;
}

mat-card:nth-child(3n) .firstLetter {
  background: #46b8da;
}

mat-card:nth-child(4n) .firstLetter {
  background: #eea236;
}

mat-card:nth-child(5n) .firstLetter {
  background: #d43f3a;
}

.delete i {
  cursor: pointer;
  color: #d43f3a;
}

.mat-space-bottom {
  margin-bottom: 10px;
  ...
}

【问题讨论】:

  • stackblitz 代码?
  • 我试试 stackblitz now。我使用 ASP.NET Core 进行后端 Crud 调用,stackblitz 可以模拟吗?我的项目你搜索书籍,现在只搜索标题作品,但是 stackblitz 应该使用 CRUD 连接但没有任何反应,我想我必须设置一个服务器

标签: css angular typescript angular-material angular8


【解决方案1】:

无法在 stackblitz 上复制该问题...也许您遗漏了什么;

请检查working stackblitz here

我使用了你的 HTML/CSS,下面是相关的 ts 文件:

import {Component} from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'card-fancy-example',
  templateUrl: 'card-fancy-example.html',
  styleUrls: ['card-fancy-example.css'],
})
export class CardFancyExample {
  bookItems:any[];
  newContact = new FormGroup({
    title: new FormControl(''),
    author: new FormControl(''),
    genre: new FormControl(''),
    price: new FormControl(''),
  });

  constructor(){
    this.bookItems = [
      { title:'test title' ,description:'test descr ' ,author:'test author' ,genre:'test1 genre' ,publish_date:'test date' ,price:'test price' }
      ,{ title:'test title' ,description:'test descr ' ,author:'test author' ,genre:'test1 genre' ,publish_date:'test date' ,price:'test price' }
      ,{ title:'test title' ,description:'test descr ' ,author:'test author' ,genre:'test1 genre' ,publish_date:'test date' ,price:'test price' }
      ,{ title:'test title' ,description:'test descr ' ,author:'test author' ,genre:'test1 genre' ,publish_date:'test date' ,price:'test price' }
      ,{ title:'test title' ,description:'test descr ' ,author:'test author' ,genre:'test1 genre' ,publish_date:'test date' ,price:'test price' }
    ];
  }
}

【讨论】:

  • 您可以通过接受答案并投票来分享您的感激之情......干杯:)
  • 我的项目来源于一个“搜索联系人”项目,我只是复制过去的学习..
  • 你的 stackblitz 看起来很简单,但我经历了你的 stackblitz 试图将它应用到我的项目中,但我无法得到与你的 stackblitz 节目相同的结果,任何想法?,卡片没有区别 -我的fancy-example.html,我检查了styles.css
  • 你使用的 npm 包版本和我一样吗?
  • 我的 @angular/... 就像 8.0.0 而你的是 8.0.1 和一些更改,我更新了我的项目但仍然没有更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 2022-08-21
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多