2016-02-29 27 views
0

我正在潛入Electron。我看到一些奇怪的行爲:在Electron中,爲什麼我不能將節點模塊的值分配給angularjs視圖?

問: 爲什麼我就當我設置從節點模塊的數據未更新$範圍對象從angularjs控制器內?

上下文: 我使用節點模塊adb-kit來檢測外部android設備。 我使用AngularJS 1.5.X來呈現自己的觀點 我使用的UI路由器設置圍繞我的看法範圍

目標:從Android設備的angularJS視圖內 顯示數據

代碼:

app.controller('DetectionController', function($scope, $state) { 
    console.log('DetectionController'); 

    //node modules 
    var adb = require('adbkit'); 
    var client = adb.createClient(); 

    //AngularJS Render Scope 
    $scope.model = { 
    id: 11111, 
    attached: true 
    } 

    var forceUpdate = function(id) { 
    console.log('call', id); 
    console.log('id type is: ', typeof id) 
    $scope.model.id = id; 
    } 

    forceUpdate(22222); 
    client.trackDevices() 
    .then(function(tracker) { 
     tracker.on('add', function(device) { 
     forceUpdate(device.id); //this doesn't assign 
     console.log('Device %s was plugged in', device.id); //shows in console 
     }); 
     tracker.on('end', function() { 
     console.log('Tracking stopped'); 
     }); 
    }) 
    .catch(function(err) { 
     console.error('Something went wrong:', err.stack); 
    }); 
}); 

控制檯輸出:

DetectionController 
detct.js:15 call 22222 
detct.js:16 id type is: number 
detct.js:15 call 0168376B1701F01C 
detct.js:16 id type is: string 
detct.js:26 Device 0168376B1701F01C was plugged in 

預期結果: 到forceUpdate()的最後呼叫應該分配0168376B1701F01C的值和更新視圖

實際結果: 的22222先前分配仍體現在DOM

問:我如何正確地從節點模塊分配一個值並讓瀏覽器更新?

enter image description here

回答

2

我懷疑問題是消化週期不會被解僱。只有在角度識別的事件期間,角纔會觸發摘要循環。如果您有自定義事件,那麼開發人員有責任讓角度知道觸發摘要循環。嘗試將代碼更改爲以下代碼

client.trackDevices() 
.then(function(tracker) { 
    tracker.on('add', function(device) { 
    forceUpdate(device.id); //this doesn't assign 
    console.log('Device %s was plugged in', device.id); //shows in console 
    $scope.$apply(); 
    }); 
    tracker.on('end', function() { 
    console.log('Tracking stopped'); 
    }); 
}) 
.catch(function(err) { 
    console.error('Something went wrong:', err.stack); 
}); 
+0

$ scope.apply()導致摘要循環錯誤 - 使用與$ timeout()相同的方法產生了正確的結果。 –

+0

@JackMurphy它是什麼消化循環錯誤它btw投擲? –

相關問題