变量won't在chrome应用中使用chrome存储API更新

Variable won't update using chrome storage API in a Chrome App

本文关键字:chrome 存储 更新 API 应用 变量 won      更新时间:2023-09-26

我正在努力处理一段代码。我是javascript的新手,我已经搜索了几天,找不到答案。

我正在使用AngularJs和Chrome Storage API编写一个Chrome应用程序,用于存储两个具有离线使用信息的对象。我已经成功地将数据写入Chrome存储,甚至已经检索到它,唯一的问题是,我存储检索数据的变量不会更新我的html页面。

这是我用来获取以前存储在Chrome存储API上的数据的代码:

   chrome.storage.local.get(null, function(result){
      app.headers = result.headersData;
      console.log('Headers recuperados');
      console.log(app.headers);
      app.maestros = result.maestrosData;
      console.log('Maestros recuperados');
      console.log(app.maestros);
  });

我的html文件有{{控制器。meestros}}和日志显示数据已被检索,但它不会反映在显示的数据上。

提前非常感谢!

是否应该将这两个值保存在$作用域中,并将HTML元素标记为与之绑定?例如

$scope.maestros = result.maestrosData

请记住,考虑到回调是在Angular的上下文之外执行的,你可能需要用

来包装你的赋值。
$scope.$apply(function() {... your assignments here...})

代码看起来像这样:

chrome.storage.local.get(null, function(result){
  $scope.$apply(function() {
    $scope.headers = result.headersData;
    $scope.maestros = result.maestrosData;
  }

});

会强制Angular从你的HTML文件中更新这些值,就像这样

<input ng-model="maestros">

你应该看到UI上的更新。

希望有帮助!