他のディレクティブと属性を動的に追加するディレクティブがあります。
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 への呼び出しを削除すると、すべてが期待どおりに機能します。しかし、このようなアプローチの大きな欠点は、属性の外観を制御できないことです。常にレンダリングされます。
私は何が欠けていますか?