3
我有指令無效的內容在那裏我動態地添加其他指令和屬性:角擦除從裏面輸入指令
app.directive('lrDatetime', function ($compile) {
return {
restrict: 'AE',
require: ["ngModel"],
scope: {
ngModel: "=",
item: "="
},
templateUrl: "template.html",
compile: function compile(element, attrs) {
var datepicker = element.find(".lr-datepicker");
if (attrs.required === "true") {
datepicker.attr("ng-required", "true");
}
return {
pre: function preLink(scope, iElement, iAttrs, controller) { },
post: function postLink(scope, iElement, iAttrs, controllers) {
$compile(iElement.contents())(scope);
}
};
},
controller: function ($scope) {
$scope.opened = false;
$scope.open = function ($event, obj, which) {
$scope.opened = true;
};
$scope.format= "dd.MM.yyyy";
}
};
});
而且模板:
<input type="text"
class="lr-datepicker"
is-open="opened"
uib-datepicker-popup="{{format}}"
datepicker-append-to-body="true"
ng-model="ngModel" />
<span class="input-group-btn">
<button type="button" class="btn btn-default"
ng-click="open($event, item, 'isOpened')">
Open
</button>
</span>
現在,當我有值綁定,並試圖輸入東西到輸入它被擦除。我知道,如果模型是無效的角度將它設置爲「未定義」,但如果我會在指令外做同樣的事情,它會保持輸入的內容。
如果我只是將這些屬性移動到模板並刪除對$ compile的調用 - 一切都按預期工作。但是這種方法的巨大減少是我無法控制屬性外觀,它總是會被渲染。
我錯過了什麼?