处理';背面';组件内部nativescript angular2中的按钮

处理';背面';组件内部nativescript angular2中的按钮,nativescript,angular2-nativescript,nativescript-angular,Nativescript,Angular2 Nativescript,Nativescript Angular,我正在尝试处理“后退”按钮,如中所示。提出了两种方法,都运行正常。我的问题是如何与组件的本地数据交互,因为“android.on”是一个全球性事件 我想做的是,当我的组件再次从另一个显示器接收到控制时(用户点击“后退”按钮关闭),刷新显示器(只需更新ngFor中引用的列表的模型) 我一直在尝试访问“ActivityBackPresseEvent”属性,但找不到访问组件数据(“shopList”)的方法 从“@angular/core”导入{Component,OnInit,ViewChild};

我正在尝试处理“后退”按钮,如中所示。提出了两种方法,都运行正常。我的问题是如何与组件的本地数据交互,因为“android.on”是一个全球性事件

我想做的是,当我的组件再次从另一个显示器接收到控制时(用户点击“后退”按钮关闭),刷新显示器(只需更新ngFor中引用的列表的模型)

我一直在尝试访问“ActivityBackPresseEvent”属性,但找不到访问组件数据(“shopList”)的方法

从“@angular/core”导入{Component,OnInit,ViewChild};
从“@angular/Router”导入{Router}”;
从“nativescript/router”导入{RouterExtensions};
从“ui/Page”导入{Page};
从“tns核心模块/数据/可观测阵列”导入{observearray};
从“nativescript ui自动完成”导入{TokenModel};
从“nativescript ui自动完成/角度”导入{RadaAutoCompleteTextViewComponent};
从“../../Utils”导入{Utils};
从“nativescript社会共享”导入*作为社会共享;
从“应用程序”导入{android,AndroidApplication,AndroidActivityBackPressedEventData};
从“tns核心模块/应用程序”导入*作为应用程序;
从“ui/frame”导入{topmost};
@组成部分({
//选择器:“购物”,
moduleId:module.id,
templateUrl:“./shopping.component.html”,
样式URL:['./shopping.component.css']
})
导出类ShoppingComponent实现OnInit{
购物清单:字符串[]=[];
构造函数(专用页:页,专用路由器:路由器,专用路由器text:RouterExtensions){
this.shopList=[];
this.shopList.push('sugar');
this.shopList.push('milk');
}
重新加载(参数:AndroidActivityBackPressedEventData):无效{
console.log(“反压”);

console.log(this.shopList);//问题是,应用程序事件
activityBackPressedEvent
是一个本地事件,而不是一个角度事件。因此,在这种情况下,要保留
this
的含义,您需要好的thisJavaScript模式

以下是如何修改代码以按预期工作:

reload(args: AndroidActivityBackPressedEventData): void {
    console.log ('back pressed');
    console.log (this.shopList);  // <<--- "[milk, sugar]"
}

ngOnInit(): void {
    let that = this;

    android.on(AndroidApplication.activityBackPressedEvent, (args: AndroidActivityBackPressedEventData) => {
        that.reload(args);
    });
}
重新加载(args:AndroidActivityBackPressedEventData):无效{
console.log(“反压”);

console.log(this.shopList);//简单而完美的解决方案!!!!非常感谢您的匹配。我不知道那种/这种模式。
reload(args: AndroidActivityBackPressedEventData): void {
    console.log ('back pressed');
    console.log (this.shopList);  // <<--- "[milk, sugar]"
}

ngOnInit(): void {
    let that = this;

    android.on(AndroidApplication.activityBackPressedEvent, (args: AndroidActivityBackPressedEventData) => {
        that.reload(args);
    });
}