Lately, I've become fixated on a particular issue that I need your help solving. My task at work involves creating a list of posts with comments, each post having the states of view, edit, and delete.
To manage the posts and comments, I've developed directives for Post and Comment, as well as PostList and CommentList directives. The code for Post and Comment is quite similar. You can view the code here: http://plnkr.co/edit/k77kdSwPnqY4jR5EY7N4?p=preview
app.directive("Post", function ($templateCache, $compile) {
function getTemplate(mode) {
switch (mode) {
case "create":
return "createPost.html";
case "view":
return "viewPost.html";
case "delete":
return "deletePost.html";
}
}
var defaultMode = "view";
return {
scope: {},
restrict: "AE",
compile: function (elem, attrs, transclude) {
return function ($scope, $element, $attr) {
function updateTemplate() {
$element.html("");
$compile($templateCache.get(getTemplate($scope.mode)).trim())($scope, function (clonedElement, scope) {
clonedElement.appendTo($element);
});
}
$scope.mode = $attr.mode || defaultMode;
$scope.$watch("mode", updateTemplate);
}
}
}
})
The templates like viewPost.html contain numerous bindings and directives such as ng-show, ng-src, ng-bind-html-unsafe. The viewPost.html includes the commentList directive for displaying comments. When compiling a Post, the comments for each post are automatically compiled.
As I render a list of 20 posts with multiple comments, the page takes over a second to load, making the webpage unresponsive during this time.
I'm starting to doubt if my chosen structure is the right way to go. Is Angular the best fit for this task?
Despite my love for Angular, I'm feeling disheartened now and worried that it might not be suitable for this project. Can you help me regain my confidence and love for Angular by pointing out what I may have done wrong?
Here is the link to my project that I'd appreciate your insights on:
login: [email protected]
pass: 123456
Please note that the content is in Russian. You can visit this page to experience the current sluggishness: .