Ionic Toggle返回错误的值

时间:2014-06-02 18:05:37

标签: angularjs ionic-framework

我使用离子切换进行了这个简单的测试但是我的警报返回False时为True,True时返回false。有什么想法吗?

http://codepen.io/anon/pen/jtKCf


$scope.pushNotificationChange = function() {
    alert('Push Notification Change: '+ $scope.pushNotification.checked);
};

1 个答案:

答案 0 :(得分:4)

您的代码很好,这恰好是一个繁琐的时间问题。 The official example here suffers the same issue as yours if you look at the console log it outputs

Here is a CodePen I made that works使用$timeout来解决此问题。

$scope.pushNotificationChange = function() {
    $timeout(function() {
        alert('Push Notification Change: '+ $scope.pushNotification.checked);
    }, 0);
};

/编辑

在看到tasseKATT的评论后,

Here is another working approach that I made

$scope.$watch('pushNotification.checked', function(newValue, oldValue) {
  console.log('Push Notification Change: ' + newValue);
});

您可以避免使用此替代方法ng-change