【问题标题】:how to save artical with html tags like <a></a> in a database angular , springboot, the article have be saved along with its title如何在数据库中保存带有 <a></a> 之类的 html 标签的文章 angular,spring boot,文章连同标题一起保存
【发布时间】:2022-12-10 16:54:25
【问题描述】:

我正在研究 angular spring boot 应用程序,所以我想保存一篇包含 html 标签(如链接)的文章。

基本上,我想保存并显示一篇带有链接的文章。我尝试使用带有角度标记的包

下面是保存文章的角度代码:

// #  component code
 savePost(){
    var form = new FormGroup({
      meid: new FormControl(this.meid, Validators.required),
      text:new 
    FormControl(marked(this.posttext),Validators.required),
      country:new FormControl(this.country,Validators.required),
    });
    if(form.valid){
      const _this = this;
      this.postsService.savePost(form.value).subscribe(
        {
         next: (res)=>{
          console.log(res);
         },
         error: (error)=>{
            console.log(error);
         },
         complete: ()=>{
            console.log("complete");
         }
         
        } 
       );
    }else{
      this._snackBar.open("You can not post empty article","", {
        duration: 1000,
      });
    }
 
  }

//service code
const headers = new HttpHeaders().set('Content-Type', 
 'application/json');
savePost(post:Object):Observable<HttpResponse<Post>> {
    return this.http.post<Post>(this.URL +"/savePost",post, { 
    observe: "response",
     headers:headers});
  }

【问题讨论】:

  • 请分享您的代码

标签: angular spring-boot api


【解决方案1】:

我弄清楚了,要保存来自角度编辑器的数据,或保存数据库中带有链接的数据,同时注意安全细节,您可以使用 Jquery 获取数据并使用角度标记来清理 html,然后保存。

更正代码:

savePost(){
var form = new FormGroup({
  meid: new FormControl(this.meid, Validators.required),
  text:new 
FormControl(marked($("#main_div_id").html()),Validators.required),
  country:new FormControl(this.country,Validators.required),
});
if(form.valid){
  const _this = this;
  this.postsService.savePost(form.value).subscribe(
    {
     next: (res)=>{
      console.log(res);
     },
     error: (error)=>{
        console.log(error);
     },
     complete: ()=>{
        console.log("complete");
     }
     
    } 
   );
}else{
  this._snackBar.open("You can not post empty article","", {
    duration: 1000,
  });
}

}

使用 [(ngModel)] 删除链接的所有标签。

在显示哟可以使用 [innerHtml]="saved_data"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-30
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多