html代码如下
<p class="form-group">
<p class="input-group">
<input type="text" class="form-control" datepicker-popup="yyyy-MM-dd HH:mm:ss" ng-model="entity.fromTime" max-date="startMaxDate" ng-change="setEndMinDate()" is-open="startOpened" datepicker-options="dateOptions" current-text="今天" clear-text="清除" close-text="关闭" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="startOpen($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</p>
</p>
controller代码:
$scope.dateOptions = {
formatYear: 'yyyy',
formatMonth:'MM',
formatDay:'dd',
startingDay: 1,
showWeeks: false,
class: 'datepicker'
};
$scope.startOpen = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.startOpened = true;
};
directive代码:
.directive('datepickerPopup', ['$compile', '$parse', '$document', '$position', 'dateFilter', 'dateParser', 'datepickerPopupConfig',
function ($compile, $parse, $document, $position, dateFilter, dateParser, datepickerPopupConfig) {
return {
restrict: 'EA',
require: 'ngModel',
scope: {
isOpen: '=?',
currentText: '@',
clearText: '@',
closeText: '@',
dateDisabled: '&'
},
link: function(scope, element, attrs, ngModel) {
scope.$watch('isOpen', function(value) {
if (value) {
scope.$broadcast('datepicker.focus');
scope.position = appendToBody ? $position.offset(element) : $position.position(element);
scope.position.top = scope.position.top + element.prop('offsetHeight');
$document.bind('click', documentClickBind);
} else {
$document.unbind('click', documentClickBind);
}
});
scope.close = function() {
scope.isOpen = false;
element[0].focus();
};
}
})
controller和directive中的isOpen是双向绑定的,但执行完directive中得scope.close方法后,第一次执行是有效的,之后就失效了,即使使用scope.$apply()也无效,求大神解答!!!
补充:在directive中定义了一个ng-click的回调方法(即scope.close)并修改了值,但在controller中再修改改值就没有作用了,不触发scope.$watch