Мне нужен доступ к глобальному значению в побочной функции успеха JavaScript (logonSuccessCallback). Как мне получить доступ к глобальному значению?Пожалуйста, предложите мне исправить это. Здесь я использую плагин cordova. Он не поддерживает ionic native.
import { Component , NgZone } from '@angular/core';
import { NavController } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { GlobalProvider } from "../../providers/global/global";
//import { LogonProvider } from "../../providers/logon/logon";
//import { Camera, CameraOptions } from '@ionic-native/camera';
//import { Device } from '@ionic-native/device';
//declare var cordova;
declare var sap: any;
declare var myExtObject: any;
declare var webGlObject: any;
//declare var globalvar;
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
todo = {};
sap:any;
globalvar:any;
slideOneForm: FormGroup;
submitAttempt: boolean = false;
constructor(public navCtrl: NavController, public formBuilder: FormBuilder,public global: GlobalProvider,private ngZone:NgZone) {//,private camera: Camera,private device: Device
//public logon:LogonProvider,
this.globalvar=global;
// this.slideOneForm = formBuilder.group({
// firstName: [''],
// lastName: [''],
// age: ['']
// })
global.appId="com.data.myapp";
this.global.context={
"serverHost": "134.87.656.193", //Place your SMP 3.0 server name here
//"serverHost": "hcpms-i82XXXXtrial.hanatrial.ondemand.com", //SAP Cloud Platform Mobile Services
"https": false, //true for SAP Cloud Platform Mobile Services
"serverPort": "8080", //443 for SAP Cloud Platform Mobile Services
//"multiUser": true,
//"useLocalStorage": false,
"user": "i82XXXX", //For demo purposes, specify the user name and password you wish to register with here to save typing on the device
"password": "XXXXX", //Note, if you wish to use this user name and password to be passed to the backend OData producer, choose Basic as the SSO mechanism
//The AuthProxy plugin with the Logon plugin can respond to 401 Authentication challenges if the same credentials used to register are also used to make OData requests
//Once set the credentials can be changed by calling sap.Logon.changePassword()
"passcode": "password", //Hardcoding passwords and unlock passcodes are strictly for ease of use during development
//Passcode can be changed by calling sap.Logon.managePasscode()
"unlockPasscode": "password",
"passcode_CONFIRM": "password",
"oldPasscode" : "password",
"communicatorId": "REST",
//"auth": [ { "type": "saml2.web.post" } ], //Indicates that a redirect to a SAML IDP should be used for registration
//"refreshSAMLSessionOnResume": "skip", // Useful for offline apps when you may not wish for a saml check to be performed when the app is resumed since you may be offline
"custom": {
"hiddenFields": ["farmId", "resourcePath", "securityConfig", "serverPort", "https"]
}
};
this.slideOneForm = formBuilder.group({
firstName: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])],
lastName: ['', Validators.compose([Validators.maxLength(30), Validators.pattern('[a-zA-Z ]*'), Validators.required])],
age: ['']
});
// save1(){
// alert("Hi");
// }
}
save(){
console.log(testvar);
console.log(this.globalvar);
this.submitAttempt = true;
if(!this.slideOneForm.valid){
alert("Failure ");
}
else{
sap.Logon.init(logonSuccessCallback, logonErrorCallback, this.global.appId, this.global.context);
}
function logonSuccessCallback(result) {
console.log(this);
console.log(result);
this.globalvar.applicationContext=result;
console.log(this.globalvar.applicationContext)
}
function logonErrorCallback(error) { //this method is called if the user cancels the registration.
console.log("An error occurred: " + JSON.stringify(error));
}
}
logForm() {
console.log(this.todo);
}
}
Глобальное значение внутри save работает нормально. При обратном вызове Calling to success это ключевое слово также печатается неопределенным.