javascript - anglarjs 绑定失效了
伊谢尔伦
伊谢尔伦 2017-04-10 16:30:41
0
1
515

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

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all (1)
Ty80

回答有误 :)

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!