As I dive into learning angularJS, I am facing a challenge in determining the best architecture for my project. My single page app is designed in such a way that the URL must always remain unchanged; I do not want users to navigate beyond the root route. The main div of my app will be used to display different views, with each new view taking over the display in this main div. These views can either be temporary or persist hidden in the DOM - I am curious to see how both scenarios play out.
I have created a basic working example of what I am aiming to achieve. You can find the demo here on Plunk. Essentially, I need to dynamically load HTML content into the DOM and allow standard angularJS controllers to interact with this new HTML. Is there a simpler or more efficient way to accomplish this without using the custom directive I am currently employing and avoiding the use of $compile() to connect to angular? Maybe there is a tool similar to a router that doesn't require changes in the URL to function?
Below is the special directive I have been utilizing, sourced from another post on Stack Overflow:
// Extracted from: http://stackoverflow.com/questions/18157305/angularjs-compiling-dynamic-html-strings-from-database
myApp.directive('dynamic', function ($compile) {
return {
replace: true,
link: function (scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(html) {
if (!html) {
return;
}
ele.html((typeof(html) === 'string') ? html : html.data);
$compile(ele.contents())(scope);
});
}
};
});
Regards,
Andy