打字稿订阅方法返回

时间:2017-03-18 13:14:04

标签: arrays angular typescript constructor

我在NG2中返回订阅数组没有什么问题。

我是打字稿的新手,我无法理解如何在函数和构造函数之间传递变量。

我的代码如下所示:

export class RosterPage extends Page {

    roster:Roster[];
    players:Players[];


    roster_data:any;

    constructor(private location: Location, private playersService: PlayersService) {
        super(location);

        this.players = [];
        this.roster = [];


        this.roster_data = this.getRoster();

        for (let i of this.roster_data) {
            console.log(i); // I need iterate 


   getRoster() {
        return this.playersService.getRoster("2017","MUZ").subscribe(roster => {
            this.roster = roster["soupiska"];
        });
    }


}

2 个答案:

答案 0 :(得分:0)

您应该在getRoster()函数中执行逻辑。

export class RosterPage extends Page {

roster:Roster[];
players:Players[];


roster_data:any;

constructor(private location: Location, private playersService: PlayersService) {
    super(location);

    this.players = [];
    this.roster = [];


    this.roster_data = this.getRoster();

    getRoster() {
       return this.playersService.getRoster("2017","MUZ").subscribe(roster => {
           this.roster = roster["soupiska"];
           for (let i of this.roster) console.log(i); // I need iterate 
       });
}

答案 1 :(得分:0)

通读How do I return the response from an asynchronous call?。它将回答关于如何处理异步调用的基本问题,例如从/向服务器获取或推送数据。

  • 您应该实现public function process_submit() { if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { return; } $can_submit_live = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); $current_post_status = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; $can_edit_approved = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); $trusted_vendor = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false; $untrusted_vendor = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false; if ( $trusted_vendor ) { header('Location: http://www.example.com/user'); }else { # code... } $text = array( 'notice' => '', 'type' => 'success' ); }并移动您想要在那里加载而不是在构造函数中执行的数据访问。
  • 我删除了那些天真无邪的部分,并没有为你的问题添加任何内容。这样就可以更清楚地说明实际情况。

public function process_submit() { 

    if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
      return; 
    }

    $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
    $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
    $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
    $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
    $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

    if ( $trusted_vendor ) $can_submit_live = true; 
    if ( $untrusted_vendor ) $can_submit_live = false; 

    if ($can_submit_live) {
      header('Location: http://www.example.com/user');
    }else {
      # code...
    }
    
    $text = array( 'notice' => '', 'type' => 'success' );
}