2016-04-15 3 views
8

मेरे पास एक जावास्क्रिप्ट फ़ंक्शन (एक कोणीय 2 मूल स्क्रिप्ट आधारित मोबाइल ऐप में) है जो एक बटन प्रेस पर ट्रिगर किया जाता है, इसे बटन को छिपाना चाहिए और इसके स्थान पर एक गतिविधि संकेतक दिखाना चाहिए, ब्लूटूथ स्कैन करें, जो बंद होने पर बंद हो जाए गतिविधि संकेतक और मूल बटन दिखाता है।वही कमांड काम करता है भले ही रीडोनली प्रॉपर्टी को असाइन करने का प्रयास किया जाए?

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; 
      } 
     }); 
} 

दुर्भाग्यवश, this.isScanning = false; लाइन इन सभी त्रुटियों को फेंकता है। मैंने क्या गल्त किया है?

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 

उत्तर

10

मुद्दा यह है कि एक बार जब आप वादा में प्रवेश करते हैं तो आप एक अलग संदर्भ में हैं; "यह" अब "इस" को इंगित करता है जिसे आपने सोचा था, इसलिए आपको "यह" एक अन्य चर में सहेजने की आवश्यकता है; कुछ लोग "उस", "स्वयं" या यहां तक ​​कि "_this" का उपयोग करते हैं ...

तो इस मुद्दे का समाधान है; ES6 विधि के लिए

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! 
      } 
     }); 
} 

अपडेट - तुम भी ES6 तीर कार्यों का उपयोग कर सकते हैं => उदाहरण के लिए आप इस के लिए पहली पंक्ति बदल सकते हैं: क्योंकि आप एक प्रयोग किया जाता

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

इस मामले में ES6 तीर फ़ंक्शन this स्वचालित रूप से मूल दायरे से होगा; और इसलिए आपको self, _this, या that चाल का उपयोग करने की आवश्यकता नहीं है।

नेटस्क्रिप्ट 2.4 ES6 के रूप में आईओएस और एंड्रॉइड दोनों में समर्थित है।

संबंधित मुद्दे