我在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"];
});
}
}
答案 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' );
}