Android的PhoneGap的应用+ AngularJS:更新$范围在Android上使用的DatePicker选定的日期 [英] Android Phonegap app + AngularJS: Update $scope with selected date on android DatePicker
问题描述
我创建了一个插件,为我的PhoneGap的应用程序,它调用一个原生的Android的DatePicker控制研究。所述的DatePicker以这种方式调用
I created a plugin for my phonegap app, which calls a native android "DatePicker" controll. The DatePicker is invoked in this way:
有是在视图上的复选框。当用户检查的复选框,我调用的DatePicker。选择一个日期和pressing确认后,事件被触发的,我得到选定的日期。问题是,这个事件里面,我没有$范围。即使我尝试设置它,它不会得到更新。我想,这种情况发生,因为当事件被触发的$范围在上下文。
There is a checkbox on the view. When the user check's the checkbox, I invoke the DatePicker. After selecting a date and pressing confirm, an event is fired an I get the date selected. The problem is, inside this event, I don't have the $scope. Even though I try to set it and it does not get updated. I imagine that this happen because the $scope is not in the context when the event is fired.
我需要用户选择做一些验证的日期,并允许他保存更改。这里是我的视图和控制器:
I need to have the date that the user selected to make some validation, and allow he to save the changes. Here is my view and controller:
查看(shortned为简单起见)
View (shortned for simplification purpose)
<div>
<span>Status</span> <br />
<label>
<!-- When this checkbox is checked, I invoke the DatePicker-->
<input type="checkbox" ng-model="resolved" ng-click="openCalendar()">
<div ></div>Item Resolved
</label>
</div>
<div class="center">
<button ng-disabled="!resolved">Save</button>
<button>Cancel</button>
</div>
控制器:
myappControllers.controller('ItemDetailsController',
['$scope', '$routeParams', 'Notification', function ($scope, $routeParams, Notification) {
$scope.notification= Notification.getNotification($routeParams.Id);
$scope.resolved = false;
$scope.date = '';
//This event is fired when the user check's the checkbox
$scope.openCalendar = function(){
// defining options
var options = {
date: new Date(),
mode: 'date'
};
//This event is fired when the user has selected a date on the DatePicker
datePicker.show(options, function(date){
$scope.date = date //Does not get updated!
});
}
//Function for save button
$scope.save = function(){
if($scope.date){
//save the changes
}
}
}]);
我该如何处理呢?
How do I handle this?
推荐答案
只需添加$范围。$适用()到你的datePicker.show方法。
Just add $scope.$apply() to your datePicker.show method.
datePicker.show(options, function(date){
$scope.date = date;
$scope.$apply();
});
角不知道的变化,因为它发生的外部,$范围。$适用()将触发一个摘要周期,因此它可以应用更改。
Angular doesn't know about the change because it happens "outside", $scope.$apply() would fire a digest cycle so it can apply the changes.
这篇关于Android的PhoneGap的应用+ AngularJS:更新$范围在Android上使用的DatePicker选定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!