Ответ 1
Вот плункер: http://plnkr.co/edit/3AFOHZFgExZKHjnd3gb0?p=preview
При замене элемента внутри функции компиляции вы должны:
- Вручную компилировать и связывать новый шаблон.
- Завершить все директивы для одного и того же элемента.
- Отметьте этот ответ: создайте новую директиву с angularjs
Директива
app.directive('smartInputElement', function($compile) {
return {
restrict: 'E',
priority: 1001,
terminal: true,
compile: function(tElm, attrs) {
var template = angular.element(
'<div class="text-input" ng-class="{typing : (' + attrs.formname + '.' + attrs.name + '.$dirty || ' + attrs.formname + '.' + attrs.name + '.length > 0)}">' +
'<span>' + attrs.label + '</span>' +
'<input type="text" name="' + attrs.name + '" ng-model="' + attrs.ngModel + '" placeholder="' + attrs.name + '">' +
'</div>');
tElm.replaceWith(template);
var fn = $compile(template);
return function(scope) {
fn(scope);
};
}
};
});