I have a requirement to render all the posts from my database by using AngularJS. I need to provide Edit functionality for each post that is visible to the user. Currently, I am doing this by using a 'edit-post' directive. Here is the linking function for that.
link: function ($scope, element, attrs) {
element.bind('click', function () {
var divId = $scope.$parent.post.meta.id + "Data";
var html = $compile("<div class='editTextAreaDiv' id='" + divId + "'>" +
"<textarea class='editTextArea' id='editBox' rows='3' ng-model='editedPostText' name='editedPostText'>" + $scope.$parent.post.meta.data + "</textarea><br />" +
"<span class='pull-right'>" +
"<input class='btn' type='button' value='Save' ng-click='saveEditedPost(\"" + divId + "\")'/>" +
"<input class='btn' type='button' value='Cancel' ng-click='cancelEdit(\"" + divId + "\")'/>" +
"</span>" +
"</div>")($scope);
$("#" + divId).html(html);
});
}
I am manipulating DOM by dynamically adding a textarea and 2 buttons.
My question is whether this approach of dynamically manipulating DOM elements is preferable in Angular world. Or should I go for some other approach (like using ng-show/ng-hide directives to show/hide the textarea and 2 buttons).
Note: I preferred not to use ng-show/ng-hide since I didn't want to introduce an extra textarea and 2 buttons for every post.
Please guide me regarding this.