我正在嘗試爲Bootstrap-UI的uib-datepicker-popup創建一個包裝AngularJS指令,所以我不必每次都重新創建一堆樣板需要選擇一個日期。 I've been working off an example I found here這是爲早期版本的Angular編寫的,並且遇到了一些可能會導致這種情況的怪事。爲Bootstrap-UI的uib-datepicker-popup創建包裝指令
我已經得到了指令,它顯示一個彈出窗口,但雙向數據綁定似乎被破壞;該字段模型中的日期值不會傳播到指令中,並且當您單擊彈出窗口並選擇一個日期時,它不會傳播回去。有沒有人對這裏發生的事情有任何想法?
I've created a Plunker demonstrating the issue here.
指令代碼:
app.directive('myDatepicker', function() {
return {
restrict: 'E',
scope: {
model: "=",
myid: "@"
},
templateUrl: 'datepicker.html',
require: 'ngModel',
link: function(scope, element) {
scope.popupOpen = false;
scope.openPopup = function($event) {
$event.preventDefault();
$event.stopPropagation();
scope.popupOpen = true;
};
scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
scope.opened = true;
};
}
};
});
模板代碼:
<div class="row">
<div class="col-md-6">
<p class="input-group">
<input type="text" class="form-control" id="{{myid}}" uib-datepicker-popup ng-model="model" is-open="opened" ng-required="true" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
你會如何將datepicker選項傳遞給指令? – flaalf