anglarjs 绑定失效了
html代码如下
<div class="form-group">
<div 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>
</div>
</div>
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答有误 :)