我创建了一个我将要执行的操作的简单示例:
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/