html代码如下
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
回答有误 :)