• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Angular使用Restful的增删改

angularjs 搞代码 4年前 (2021-12-31) 41次浏览 已收录 0个评论

今天小编就为大家分享一篇关于Angular使用Restful的增删改,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

这篇来看一下如何进行增删改。

删除

使用delete进行删除,一般页面设计的时候也基本都是在列表页进行操作的。首先为删除的链接添加一个函数,因为一般删除都需要传入可定位删除的id或者name,前提是后端api是否支持,查看如下的调用之后,可以看到:

所以,只需要method使用delete,在传入的url中指来源gaodaimacom搞#代%码网定id或者name即可。

删除的restful调用:https://docs.konghq.com/0.13.x/admin-api/#delete-api

模版修改

html页面做如下修改

 <i class="anticon anticon-minus-circle-o"></i>

添加click处理函数

添加页面定义的click处理函数handleDeleteFunc:

 handleDeleteFunc(apiName) { this._actionInformation = 'Delete'; this.isSpinning = true; this.modalService.confirm({ nzTitle : '<i>Are you sure to delete this item selected?</i>', nzContent: '<b>The api selected will be deleted.</b>', nzOnOk  : () => { this.http.delete('/apis/' + apiName.toString()).subscribe( item => { this.isSpinning = false; this._getApis(); } ); } }); }

添加&更新&查看

其他操作诸如添加/更新/查看, 这样基本上get/delete/post/put都进行了使用

TS文件

 import { Component, OnInit } from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; import { NzModalService } from 'ng-zorro-antd'; export class ApiModel { created_at: string; strip_uri: boolean; id: string; hosts: ['']; name: string; http_if_terminated: boolean; https_only: boolean; retries: number; preserve_host: boolean; upstream_connect_timeout: number; upstream_read_timeout: number; upstream_send_timeout: number; upstream_url: string; } @Component({ selector: 'app-rest-demo', templateUrl: './rest-demo.component.html', styleUrls: ['./rest-demo.component.css'] }) export class RestDemoComponent implements OnInit { dataModel = []; isModalVisible = false; _actionInformation: string; _dataSelected: ApiModel; isSpinning = true; public httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) }; constructor(private http: HttpClient, private modalService: NzModalService) { } ngOnInit() { this._getApis(); this._initData(); } _initData() { this._dataSelected = new ApiModel(); this._dataSelected.upstream_connect_timeout = 6000; this._dataSelected.retries = 5; } _getApis() { this.isSpinning = true; this.http.get('/apis').subscribe( item => { this.dataModel = item['data']; this.isSpinning = false; } ); } handleAddFunc() { this._actionInformation = 'Add'; this.isModalVisible = true; } handleSearchFunc(apiName) { this._actionInformation = 'Search'; this.http.get('/apis/' + apiName).subscribe( item => { this._dataSelected =  item; this.isSpinning = false; } ); this.isModalVisible = true; } handleDeleteFunc(apiName) { this._actionInformation = 'Delete'; this.isSpinning = true; this.modalService.confirm({ nzTitle : '<i>Are you sure to delete this item selected?</i>', nzContent: '<b>The api selected will be deleted.</b>', nzOnOk  : () => { this.http.delete('/apis/' + apiName.toString()).subscribe( item => { this.isSpinning = false; this._getApis(); } ); } }); } handleEditeFunc(apiName) { this._actionInformation = 'Edit'; this.http.get('/apis/' + apiName).subscribe( item => { this._dataSelected =  item; this.isSpinning = false; } ); this.isModalVisible = true; } handleOperationCancel() { this.isModalVisible = false; } handleOperationOk() { this.isSpinning = true; this.isModalVisible = false; if (this._actionInformation === 'Add') { this.http.post('/apis/', JSON.stringify(this._dataSelected), this.httpOptions).subscribe( item => { this.isSpinning = false; this._getApis(); }); } else if (this._actionInformation === 'Edit') { this.http.put('/apis/', JSON.stringify(this._dataSelected), this.httpOptions).subscribe( item => { this.isSpinning = false; this._getApis(); }); } else if (this._actionInformation === 'Search') { } } }

HTML模版

 <div style="width: 50%">  OperationsApis</div><div style="width: 45%;text-align: right;margin-right: 5%;line-height: 40px;font-size: xx-large">  <i style="text-align: right" class="anticon anticon-plus-circle-o"></i></div><br> <thead> <tr> <th>Name</th><th>Host</th><th>Https only</th><th>Retry Cnt</th><th>Upstream url</th><th>Created</th><th>Operation</th></tr></thead><tbody> <tr> <td>{{data.name}}</td><td>{{data.hosts}}</td><td>{{data.https_only}}</td><td>{{data.retries}}</td><td>{{data.upstream_url}}</td><td>{{data.created_at | date:'yyyy/MM/dd HH:MM:SS'}}</td><td> <i class="anticon anticon-minus-circle-o"></i>|<i class="anticon anticon-edit"></i>|<i class="anticon anticon-exclamation-circle-o"></i></td></tr></tbody>   Name Host    <label>Https </label>Retry  Url 

总结

以上就是Angular使用Restful的增删改的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Angular使用Restful的增删改

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址