[英]How to disable refresh page after click on Save button in bootstrap modal?
I am using bootstrap modal in angular application to add new author.我在 angular 应用程序中使用引导模式来添加新作者。 When i click on saveAuthor() every time page refresh and it also refresh when i click delete button ?
当我每次刷新页面时点击 saveAuthor() 并且当我点击删除按钮时它也会刷新? I also use modal in modal in my application.
我也在我的应用程序中使用 modal in modal 。 When I open the second modal and want to save changes the page is also refreshed and both modals are closed.
当我打开第二个模态并想要保存更改时,页面也会刷新并且两个模态都关闭。 Is there any attribute on button which can fix it ?
按钮上是否有任何属性可以修复它? How can i disable page refreshing ?
如何禁用页面刷新?
My code:
<div class="button">
<button type="button" class="btn btn-info"
style="margin-top: 20px; margin-left: -20px"
data-toggle="modal"
data-target="#addAuthor"
data-backdrop="static" data-keyboard="false">Add Author
</button>
</div>
<table class="table table-striped" style="margin-top: 50px; width: 45%; border-radius: 10px !Important">
<thead>
<tr>
<th scope="col"></th>
<th scope="col"><h4> Author name </h4></th>
<th scope="col"><h4> Adress </h4></th>
<th scope="col"><h4 style="padding-left: 35px"> Actions </h4></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let author of authorList">
<th scope="row"></th>
<td><b>{{ author.name }}</b></td>
<td><b>{{ author.address }}</b></td>
<td>
<button style="width: 70px" class="btn btn-outline-info" (click)="editAuthor()">Edit</button>
<span style=" padding-left: 10px">
<button style="width: 70px;" class="btn btn-outline-danger"
(click)="deleteAuthor(author.id)">Delete
</button>
</span>
</td>
</tr>
</tbody>
</table>
<!-- ADD AUTHOR-->
<div class="modal fade" id="addAuthor" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add Author</h5>
<button class="close" id="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" required [(ngModel)]="author.name" name="name">
</div>
<div class="form-group">
<label for="address">Address</label>
<input type="text" class="form-control" id="address" required [(ngModel)]="author.address" name="address">
</div>
</form>
<div class="modal-footer">
<button type="button" class="btn btn-info" id="btn" data-dismiss="modal"
(click)="saveAuthor()">OK
</button>
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
Typescript:打字稿:
import {Component, OnInit} from '@angular/core';
import {Author} from '../author';
import {AuthorService} from '../author.service';
import {Router} from '@angular/router';
@Component({
selector: 'app-author',
templateUrl: './author.component.html',
styleUrls: ['./author.component.css']
})
export class AuthorComponent implements OnInit {
authorList: Author[] = [];
author: Author = new Author();
constructor(private authorService: AuthorService, private router: Router) {
}
ngOnInit(): void {
this.getAuthors();
}
getAuthors(): void {
this.authorService.getAuthorList()
.subscribe(res => {
this.authorList = res;
},
error1 => console.log(error1)
);
}
saveAuthor(): void {
this.authorService.createAuthor(this.author)
.subscribe(
createdAuthor => this.authorList.push(createdAuthor),
error => console.log(error));
}
editAuthor(): void {
}
deleteAuthor(id: number): void {
if (confirm('Are you sure to delete this author ?')) {
this.authorService.deleteAuthor(id)
.subscribe(
data => {
this.authorList.slice(id);
},
err => console.log(err));
}
}
}
You'll need to prevent the default of the button.您需要阻止按钮的默认设置。 Check this: Event.preventDefault()
检查这个: Event.preventDefault()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.