如何将社交分享添加到帖子Ionic wordpress app

时间:2018-01-21 14:29:24

标签: android json wordpress cordova ionic-framework

我一直在尝试为我的应用中的帖子插入一个分享按钮,该帖子能够使用Android手机中安装的默认应用,似乎无法找到方法。

这是我的post.ts文件的样子

import { Component } from '@angular/core';
import { NavParams, NavController, AlertController } from 'ionic-angular';
.
.
import { SocialSharing } from '@ionic-native/social-sharing';


/**
 * Generated class for the PostPage page.
 */


@Component({
  selector: 'page-post',
  templateUrl: 'post.html'
})
export class PostPage {

  post: any;
  user: string; 
  comments: Array<any> = new Array<any>();
  categories: Array<any> = new Array<any>();
  morePagesAvailable: boolean = true;

  constructor(
    public navParams: NavParams,
    public navCtrl: NavController,
    public alertCtrl: AlertController,
    private socialSharing: SocialSharing
  ) {

  }

  ionViewWillEnter(){
    this.morePagesAvailable = true;

    this.post = this.navParams.get('item');

    Observable.forkJoin(
      this.getAuthorData(),
      this.getCategories(),
      this.getComments())
      .subscribe(data => {
        this.user = data[0].name;
        this.categories = data[1];
        this.comments = data[2];
      });
  }

  getAuthorData(){
    return this.wordpressService.getAuthor(this.post.author);
  }

  getCategories(){
    return this.wordpressService.getPostCategories(this.post);
  }

  getComments(){
    return this.wordpressService.getComments(this.post.id);
  }

  loadMoreComments(infiniteScroll) {
    let page = (this.comments.length/10) + 1;
    this.wordpressService.getComments(this.post.id, page)
    .subscribe(data => {
      for(let item of data){
        this.comments.push(item);
      }
      infiniteScroll.complete();
    }, err => {
      console.log(err);
      this.morePagesAvailable = false;
    })
  }

  goToCategoryPosts(categoryId, categoryTitle){
    this.navCtrl.push(HomePage, {
      id: categoryId,
      title: categoryTitle
    })
  }


// Social sharing function is here

 sharePost() {

    this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL")
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

}

问题 如何插入帖子标题,将网址图片(REST API - JSON)发布到this.socialSharing.share("Post Excerpt", "Post Title", "Post Image URL", "Post URL") 这样分享按钮看起来更像是

<button ion-fab class="btn share" mini (click)="sharePost()">&#xF497;</button>

修改 我已设法使用

使其工作
sharePost() {

    this.socialSharing.share(this.post.excerpt.rendered, this.post.title.rendered, this.post.images.large, this.post.link)
    .then(() => {
      console.log("sharePost: Success");
    }).catch(() => {
      console.error("sharePost: failed");
    });

  }

但是,当我分享使用gmail时,html特殊字符会显示

e.g title shows: catering &#038; Cleaning Services 
Excerpt shows:   <p>Some text[&hellip;]</p>

我如何摆脱那些HTML字符,只显示一些干净的文字。?

谢谢

1 个答案:

答案 0 :(得分:0)

我从wordpress帖子中删除html标签的一种方法是创建一个管道,然后在将其摘录呈现给视图之前,将摘录通过该管道

Pipe.ts就是这样

import { Pipe, PipeTransform } from '@angular/core';

/**
 * Generated class for the RemovehtmltagsPipe pipe.
 *
 * See https://angular.io/api/core/Pipe for more info on Angular Pipes.
 */
@Pipe({
  name: 'RemovehtmltagsPipe',
})
export class RemovehtmltagsPipe implements PipeTransform {
  /**
   * Takes a value and makes it lowercase.
   */
  transform(value: string) {
    if (value) {
      let result = value.replace(/<\/?[^>]+>/gi, "");
      return result;
    }
    else {

    }
  }
}

然后我将管道作为导出添加到组件的模块中。

details.module.ts就是这样

import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; 
import { IonicPageModule } from 'ionic-angular';
import { DetailsPage } from './details';
import { RemovehtmltagsPipe } from 
'../../pipes/removehtmltags/removehtmltags';

@NgModule({
  declarations: [
    DetailsPage,
  ],
  imports: [
    IonicPageModule.forChild(DetailsPage),
  ],
  exports: [RemovehtmltagsPipe],
  schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class DetailsPageModule {}

最终在我的html代码中使用了管道

details.html

 <ion-row class="white-bg" padding>
    <ion-col>
        <h1 class="title">{{article.title.rendered | RemovehtmltagsPipe}}</h1>
        <p class="date">Published: {{article.modified.split('T')[0]}}  {{article.modified.split('T')[1]}}</p>
    </ion-col>
</ion-row>

这应该删除您帖子中的所有html标签。希望这会有所帮助