在 Electron 中,为什么我不能将节点模块的值分配给 angularjs 视图?

In Electron, why can't i assign values from node modules to angularjs views?

我正在研究 Electron。我看到一些奇怪的行为:

问题: 当我从 angularjs 控制器中的节点模块设置数据时,为什么我在 $scope 上的对象没有更新?

上下文: 我正在使用节点模块 adb-kit 来检测外部 android 设备。 我正在使用 AngularJS 1.5.X 来呈现我的观点 我正在使用 ui-router 来围绕我的视图设置范围

目标: 在 angularJS 视图

中显示来自 android 设备的数据

代码:

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

提问:如何从节点模块正确赋值并让浏览器更新?

我怀疑问题出在摘要循环没有被触发。 Angular 将仅在 angular 识别的事件期间触发摘要循环。如果您有自定义事件,那么开发人员有责任让 angular 知道触发摘要循环。尝试将代码更改为以下内容

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