2016-04-15 11 views
8

Bir tuşa basıldığında tetiklenen bir Javascript işlevi (Angular 2 NativeScript tabanlı bir mobil uygulamada) var, düğmeyi gizlemeli ve yerine bir etkinlik göstergesi göstermeli, tamamlandığında Bluetooth taraması gerçekleştirmeli. etkinlik göstergesi ve orijinal butonu gösterir.Başka bir yerde aynı komutlar çalışmasına rağmen readonly özellik atamayı denediniz mi?

bluetoothAdd() { 
    this.isScanning = true; 
    var plusIcon = this.page.getViewById("add"); 
    plusIcon.style.opacity = 0; 
    bluetooth.hasCoarseLocationPermission().then(
     function (granted) { 
      if (!granted) { 
       bluetooth.requestCoarseLocationPermission(); 
      } else { 
       bluetooth.startScanning({ 
        serviceUUIDs: ["133d"], 
        seconds: 4, 
        onDiscovered: function (peripheral) { 
         console.log("Periperhal found with UUID: " + peripheral.UUID); 
        } 
       }).then(function() { 
        console.log("scanning complete"); 
        this.isScanning = false; 
        plusIcon.style.opacity = 1; 
       }, function (err) { 
        console.log("error while scanning: " + err); 
       }); 
       this.isScanning = false; 
      } 
     }); 
} 

Maalesef this.isScanning = false; çizgi bu hataların hepsi atar. Neyi yanlış yaptım?

CONSOLE ERROR file:///app/tns_modules/angular2/src/platform/server/parse5_adapter.js:55:75: EXCEPTION: Error: Uncaught (in promise): TypeError: Attempted to assign to readonly property. 
CONSOLE ERROR file:///app/tns_modules/angular2/src/platform/server/parse5_adapter.js:53:75: STACKTRACE: 
CONSOLE ERROR file:///app/tns_modules/angular2/src/platform/server/parse5_adapter.js:53:75: [email protected]:///app/tns_modules/zone.js/dist/zone-node.js:496:41 
file:///app/tns_modules/zone.js/dist/zone-node.js:532:32 
[email protected]:///app/tns_modules/zone.js/dist/zone-node.js:314:43 
[email protected]:///app/tns_modules/angular2/src/core/zone/ng_zone_impl.js:35:51 
[email protected]:///app/tns_modules/zone.js/dist/zone-node.js:313:55 
[email protected]:///app/tns_modules/zone.js/dist/zone-node.js:214:58 
[email protected]:///app/tns_modules/zone.js/dist/zone-node.js:432:43 
[email protected][native code] 
[email protected][native code] 
[email protected]:///app/tns_modules/application/application.js:233:26 
file:///app/tns_modules/nativescript-angular/application.js:65:26 
[email protected]:///app/tns_modules/zone.js/dist/zone-node.js:542:38 
[email protected]:///app/tns_modules/nativescript-angular/application.js:64:23 
[email protected]:///app/main.js:5:36 
[email protected][native code] 
[email protected][native code] 
[native code] 
[email protected][native code] 
CONSOLE ERROR file:///app/tns_modules/zone.js/dist/zone-node.js:419:27: Unhandled Promise rejection: Attempted to assign to readonly property. ; Zone: angular ; Task: Promise.then ; Value: TypeError: Attempted to assign to readonly property. 
CONSOLE ERROR file:///app/tns_modules/zone.js/dist/zone-node.js:421:23: Error: Uncaught (in promise): TypeError: Attempted to assign to readonly property. 
CONSOLE LOG file:///app/Pages/Home/home.component.js:99:32: scanning complete 

cevap

10

Sorun, Söze girdiğinizde, farklı bir bağlamda olduğunuzu; “bu” artık düşündüğünüz “bu” ya işaret etmez, bu yüzden “bunu” başka bir değişkene kaydetmelisiniz; Bazı insanlar "bu", "kendi kendine" ve hatta "_this" kullanırlar ...

Bu sorunun çözümü; Bir kullanıldığından Bu durumda

bluetooth.hasCoarseLocationPermission().then(
      (granted) => { 

: - ES6 yöntemi için

bluetoothAdd() { 
    this.isScanning = true; 
    var plusIcon = this.page.getViewById("add"); 
    plusIcon.style.opacity = 0; 

    var self = this; // THIS LINE ADDED 

    bluetooth.hasCoarseLocationPermission().then(
     function (granted) { 
      if (!granted) { 
       bluetooth.requestCoarseLocationPermission(); 
      } else { 
       bluetooth.startScanning({ 
        serviceUUIDs: ["133d"], 
        seconds: 4, 
        onDiscovered: function (peripheral) { 
         console.log("Periperhal found with UUID: " + peripheral.UUID); 
        } 
       }).then(function() { 
        console.log("scanning complete"); 
        self.isScanning = false; // CHANGED! 
        plusIcon.style.opacity = 1; 
       }, function (err) { 
        console.log("error while scanning: " + err); 
       }); 
       self.isScanning = false; // CHANGED! 
      } 
     }); 
} 

Güncelleme Ayrıca => Örneğin bu ilk satırı değiştirebilir ES6 ok işlevlerini kullanabilirsiniz ES6 ok işlevi this otomatik olarak ana kapsamdan olacaktır; ve böylece self, _this veya that numaralarını kullanmanıza gerek yoktur.

NativeScript'ten 2.4 ES6 hem iOS hem de Android'de desteklenir.

İlgili konular