I created an Angular directive to provide a means of attaching an ng-if directive and erase the element, replacing it with its content. I think this should be much easier, possibly using transclusion, but I can't quite work it out. How should I do this?
angular.module('myApp', []).
directive('tyReplace', function () {
return {
restrict: 'A',
replace: true,
scope: { tyReplace: '@' },
link: function (scope, element) {
element.parent().text(scope.tyReplace);
}
}
});
Usage:
<td>
<div ty-replace="{{content}}" ng-if="condition"></div>
<ul ng-if="othercondition">
<li ng-repeat="item in items">{{item}}</li>
</ul>
</td>
I started adding additional display options within the <td>, but we also allow certain cells to be edited by toggling the contenteditable attribute. This approach allows me to continue providing that option.
EDIT
Very soon, I would like to be able to replace {{content}} with something more complex, such as an <input type="text" /><input type="datetime" /> for text and date controls when editing. The current solution won't work when I want more complex markup inside.
td) innerHTML to be replaced with the contents of thety-replacevariable?<div ty-replace ng-if="">{{content}}</div>, but I couldn't get the content to lift into the parent's content.{{content}}with something more complex, how and where from will the markup be generated?