0

我正在为一段代码而苦苦挣扎。我是 javascript 新手,我已经搜索了几天,但找不到答案。

我正在使用 AngularJs 和 Chrome 存储 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 文件有 {{controller.maestros}} 并且日志显示数据已被检索,但它不会反映显示的数据。

非常感谢您!

4

1 回答 1

0

您不应该将这两个值保存在 $scope 中并将您的 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 上看到更新。

希望能帮助到你!

于 2014-12-09T17:52:11.017 回答