javascript - OnsenUI AngularJS 数据绑定(bind)无法正常工作

标签 javascript angularjs cordova onsen-ui 2-way-object-databinding

我创建了一个我将要执行的操作的简单示例:

ons.bootstrap()
  .controller('AppController', function($scope) {
    $scope.x = 0;
    setInterval(function() {
      $scope.x = $scope.x + 1;
    }, 500)
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/onsen/1.3.15/js/onsenui.min.js"></script>

<ons-page ng-controller="AppController">
  <ons-list>
    <ons-list-header>Switch</ons-list-header>
    <ons-list-item>
      <br/>
      This value should change every 500 milisecond: {{ x }}
      <br/>
    </ons-list-item>
    <ons-list-item>
      <div class="center">
        Switch is {{ switch ? 'on' : 'off' }}
      </div>
      <div class="right">
        <ons-switch ng-model="data.switch"></ons-switch>
      </div>
    </ons-list-item>
  </ons-list>
</ons-page>

作为一个普通的 Angular (使用 two-way data binding )应用程序, Controller 中的任何更改都会导致 View 更新,反之亦然。 但结合OnsenUI View 更新仅在一个温泉组件更改更新 View 时发生。有什么方法可以通知温泉发生了什么(需要查看更新)。

最佳答案

这样试试

ons.bootstrap()
.controller('AppController', function($scope) {
 $scope.x = 0;
  setInterval(function() {
   $scope.$evalAsync(function () {
                $scope.x = $scope.x + 1;
            });

 }, 500)
});

编辑:

或者使用 $interval 正如@Pankaj Parkar 所说的那样。在使用 $interval 之前你应该在你的 Controller 中注入(inject)这个服务。

 $interval(function () {
   $scope.x = $scope.x + 1;               
 }, 500);

关于javascript - OnsenUI AngularJS 数据绑定(bind)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247244/

相关文章:

javascript - jquery.i18n.properties 不工作

angularjs - jwt中过期时如何刷新 token

javascript - 无法使用通过 ng-if 创建的子作用域设置父变量

javascript - 将项目升级到最新版本的 Cordova 4.0.0 时,控制台出现 TypeError "Cannot set property connection of#<Navigator>"

cordova - 使用 Azure 应用服务移动应用或 Azure 移动服务离线同步 SQLite(Xamarin、Ionic、PhoneGap)

cordova - 在 Cordova 项目上运行 Karma 时执行 0 of 0 ERROR

java - Android - 将值从 HTML 或 Javascript 传递到 Java 的最简单方法?

javascript - 将 JavaScript 用 let 转换为 var

javascript - 主干事件获取元素类型

javascript - 从不同的 javascript/Jquery 函数调用 Angularjs 函数