edit-user.component.ts
在我们页面引入相关组件
import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {NoteServiceService, NoteDto, UpdateNoteDto} from '@app/blog/note-service.service';
import { AppComponentBase } from '@shared/app-component-base';
定义一些东西
export class EditNoteComponent extends AppComponentBase {
active = false; // 弹出层内容是否有效
note: UpdateNoteDto; // 编辑的文章
preViewContent = ''; // 文章预览内容,转换层html后的
@ViewChild('editNoteModal') modal: ModalDirective; // 弹出层
@ViewChild('modalContent') modalContent: ElementRef; // 弹出层内的内容
@Output() modalSave: EventEmitter<any> = new EventEmitter<any>(); // 页面间传值,这相当于一个自定义事件
constructor(injector: Injector, private noteServer: NoteServiceService) {
super(injector);
}
// 显示
show(id: number): void {
this.noteServer.GetNote(id).subscribe(m => {
this.note = m;
this.active = true;
this.modal.show();
});
}
// 关闭
close(): void {
this.updateNote();
this.active = false;
this.modal.hide();
}
// 更新
updateNote(): void {
this.noteServer.Update(this.note).subscribe(m => {
});
}
}
edit-note.component.html编辑页面初步布局如下
<div bsModal #editNoteModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="editNoteModal" aria-hidden="true" [config]="{backdrop: 'static'}">
<div class="container">
<div #modalContent>
<div *ngIf="active">
<div class="m-editer-area">
<!--************* 顶部,文章标题和一些菜单 ****************-->
<header>
<div class="u-title">
<input type="text" [(ngModel)]="note.title" class="form-control">
</div>
</header>
<!--************* 中间部分,编辑和预览区域 ****************-->
<section>
<!--************* 中间部分左边,编辑区域 ****************-->
<div class="u-wirte z-half">
<textarea id="editer" [(ngModel)]="note.content"></textarea>
</div>
<!--************* 中间部分右边,预览区域 ****************-->
<div class="u-view z-half">
<article [innerHTML]="preViewContent"></article>
</div>
</section>
<!--************* 底部, ****************-->
<footer>
<div class="u-count">
<p>共{{note.content.length}}字节</p>
</div>
<div class="u-menu">
<a>发布</a>
<a (click)="close()">关闭</a>
</div>
</footer>
</div>
</div>
</div>
</div>
</div>
我们在edit-note.component.css写点样式
.m-editer-area {
position: absolute;
top: 10%;
left: 10%;
right: 10%;
background: rgba(91, 28, 111, 0.8);
transition: all .5s;
}
.m-editer-area header, .m-editer-area footer {
text-align: right;
font-size: 0;
}
.m-editer-area a {
display: inline-block;
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
font-size: 15px;
cursor: pointer;
color: #fff;
transition: all .5s;
}
.m-editer-area a:hover {
background: rgba(0,0,0,0.2);
}
.m-editer-area a.z-atv {
color: #dd3c1f;
background: rgba(255,255,255,0.8);
}
.m-editer-area footer a {
padding: 0 10px;
}
.m-editer-area footer a:active, a.u-screen-tab:active {
background: #dd3c1f;
color: #fff;
transition: none;
}
.m-editer-area footer p {
float: left;
line-height: 50px;
margin-left: 15px;
color: #fff;
}
.m-editer-area section {
position: relative;
height: 400px;
background: #fff;
overflow: hidden;
}
.m-editer-area section>div {
transition: all .5s;
}
.u-wirte {
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
.u-wirte textarea {
width: 98%;
height: 100%;
padding: 0 1%;
line-height: 150%;
border: none;
outline: none;
resize: none;
font-size: 15px;
font-family: "微软雅黑"
}
.u-view {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
overflow: auto;
padding: 1%;
background: #fff;
box-shadow: 0 0 10px #000 inset;
transform: translateX(100%);
}
.u-view article {
padding: 0;
background: transparent;
}
.u-view.z-half {
left: 50%;
transform: translateX(0);
}
.u-wirte.z-half {
right: 50%;
}
.u-view.z-atv {
transform: translateX(0);
}
.z-full {
top: 0;
right: 0;
bottom: 0;
left: 0;
margin-top: 0;
}
.z-full section {
position: absolute;
top: 50px;
left: 0;
bottom: 50px;
right: 0;
height: auto;
}
.z-full footer {
position: absolute;
bottom: 0;
width: 100%;
}
header {
padding:10px 0;
}
header .u-title{
width: 65%;
}
header .u-title input{
background-color: transparent;
color: whitesmoke;
border: none;
}
footer .u-count{
width: 20%;
float: left;
color: white;
font-size: 16px;
}
footer .u-menu{
width: 70%;
float: right;
}
footer .u-menu *{
float: right;
}
引用编辑功能
在note.component.ts中如下使用。添加引用,添加编辑方法。
import {EditNoteComponent} from '@app/blog/note/edit-note/edit-note.component';
@ViewChild('editNoteModal') editNoteModal: EditNoteComponent;
editNote(note: NoteDto) {
this.editNoteModal.show(note.id);
}
在 note.component.html中添加模块并调用editNote()
<ul class="dropdown-menu pull-right">
<li><a href="javascript:void(0);" class="waves-effect waves-block" (click)="editNote(note)"><i class="material-icons">create</i>编辑</a></li>
<li><a href="javascript:void(0);" class="waves-effect waves-block" (click)="delete(note)"><i class="material-icons">delete_sweep</i>删除</a></li>
</ul>
<app-edit-note #editNoteModal ></app-edit-note>