AngularJS single-page application with model-view-controller style designs

Hey there, I'm relatively new to AngularJS and currently on a steep learning curve.

I've been working on developing an AngularJS SPA and have grasped the basics. I'm using ngRoute for routing and have put together a basic application framework.

However, I've run into a roadblock which may be due to my limited knowledge of AngularJS. I'm trying to implement something similar to MVC layouts in my SPA application. What I envision is a setup like this:

Login.html - without a predefined layout, which will be specified on this page Home.html - utilizes layout.tpl.html with the remaining content defined in Home.html

...and so forth. For example, for Home.html I would want to do something like this:

<div layout="layout.tpl.html">
...rest of the content
</div>

The contents of layout.tpl.html could be as follows:

<div class="container">
  ..layout content including header, left navigation, etc.
  <div class="content">
    <div layout-content></div>
  </div>
</div>

As mentioned earlier, I am using ngRoute in my module to set up the route provider. I'm aiming to achieve something akin to:

@ {
Layout = "_Layout.cshtml"; // or Layout = null;
}

But in AngularJS. Any suggestions on how to accomplish this would be greatly appreciated!

Answer №1

There are a couple of methods to determine when ng-include has finished loading:

1) You can use the onload attribute for inline expressions. For example:

<div ng-include="'template.html'" onload="loaded = true"></div>

2) Another option is using the $includeContentLoaded event that is emitted by ng-include, which allows for application-wide handling. For instance:

app.run(function($rootScope){
  $rootScope.$on("$includeContentLoaded", function(event, templateName){
     //...
  });
});

Alternatively,

You can also utilize a directive to address this issue.

HTML

<div custom-include url="{{url}}"></div>

Directive

app.directive('customInclude', ['$http', '$compile', '$timeout', customInclude]);
    function customInclude($http, $compile, $timeout) {
        return {
            restrict: 'A',
            link: function link($scope, elem, attrs) {
                //if url is not empty
                if (attrs.url) {
                    $http({ method: 'GET', url: attrs.url, cache: true }).then(function (result) {
                        elem.append($compile(angular.element(result.data))($scope));
                        //after sometime we add width and height of modal
                        $timeout(function () {
                            //write your own code
                        }, 1, false);
                    });
                }
            }
        };
    }

OR:

<div ng-include src="template.html"></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Interested in learning how to redirect to a different page using vanilla JavaScript after submitting an HTML form with a Django backend?

Recently delving into Django, I encountered a challenge of linking a js file to my HTML form page and saving the form data before moving on to the next screen. My aim was to incorporate a feature where users can click a picture and POST it along with their ...

I need to update the class definition of a navigation menu item that contains an "a" tag with the class "section-link". How can I dynamically add the class "popover-link-a" to this definition using JavaScript?

If the grid in the body contains no data, then I want to display a pop-up message on the menu li element. This pop-up is triggered by adding the class popover-link-a. The current setup looks like this: <li id="tecrube" runat="server" ...

Combine Firebase and Stamplay services to leverage the power of Firebase Authorization in conjunction with Stamplay's Stripe integration

After successfully developing an App with Firebase as the backend, my next step is to implement Stripe for charging users and sending payments (or PayPal if it's a suitable alternative). Delving into solutions online, I've discovered that most of ...

Incorporating external JavaScript into a Rails application

Having trouble with the syntax on this simple problem. Struggling to find examples of externally linked files, all solutions online involve storing a local copy instead. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" typ ...

Issue with 1.bundle.js not loading during webpack production build with routing

I have been encountering an issue with my test repository for this specific problem (Link) It appears that the problem lies within the localization file, as I am using react-intl. The development version seems to be functioning properly. This is what&ap ...

Use the Messages.properties file instead of using json files with the angular translate static file loader

Is it possible to utilize Messages.properties files in place of json files when using the angular translate static file loader? Are there alternative file extensions, besides json, that can be employed? ...

Managing multiple sets of data in a structured form similar to an array

How Do I Send Form Data as an Array? Take a look at the code snippet below. I'm having trouble setting the index in product_attribute['index must be here']['key'] <tr v-for="index in attributes"> <td class="text-left ...

A guide to using select2.js to activate a selection based on a data attribute

I'm faced with the following html snippet: <div class='multiWrapper'> <select id="multi" class="js-dropdown-from-code"> <optgroup class='def-cursor' label='Code' data-city ...

unable to respond when clicking an angularjs link

I'm facing an issue where I can't get a link to respond to click events in AngularJS. When I click on the anchor link, nothing happens. Here is a snippet of the AngularJS script: <script data-require="<a href="/cdn-cgi/l/email-protection" ...

Retrieve the origin of the copied text

While working on a Vue application, I'm curious to know if it's possible to access the source of pasted text. For example, whether the pasted text originated from your own application, Word, or Notepad? I attempted the code below but was unable ...

Tips for selecting the initial and final elements of a specific class

Here is a simple example of table markup: <div class="table"> <div class="table__headers"></div> <div class="table__row"></div> <div class="table__row"></div> </div& ...

Can you explain the purpose of the square brackets within the ".module("modulename", [...])" syntax used in AngularJS?

I recently came across a sample demonstrating routing in AngularJS. I am curious about the connection between the dependency 'ngRoute' and the module mainApp, as shown in the syntax var mainApp = angular.module("mainApp", ['ngRoute']);. ...

Stop closing the bootstrap modal popup when the space key is pressed

Is there a way to prevent the model popup from closing when the space or enter key is pressed on the keyboard? I have already tried using data-backdrop="static" data-keyboard="false" but it still closes. Additionally, I have ensured that the form tag is no ...

The function is returning the text rather than an object through my service

I have developed a service in my application to share an object. However, when I attempt to post that object to the mongo db and call the function that should return the object, it only gives me the function's text. Below is the code for the service: ...

``There is an issue with the onsubmit property that prevents the opening of

I have encountered an issue with my forms that has me stumped. Despite searching online for help, I can't seem to figure out why my implementation is not working as intended. The concept is straightforward. I've embedded a form within a JSP page ...

Passing values from a Laravel controller to a Vue component as a prop

At the moment, I have a Laravel route that directs to the index function of my controller with an ID passed, where I then return the ID in a view. public function index($id) { return view('progress') ->with('identifier', ...

Send dropdown selections to a Javascript function and convert them into an array

Within my JavaScript function, I need to pass multiple selected values from dropdown menus and store them in a JavaScript array. Each time a value is selected and sent using the onchange() function, it should be added to the array. If the same value is sel ...

Executing AJAX requests to trigger a function in a separate MVC controller using Jquery

Below is the structure of my folders. Both of these folders are located within the Area folder. https://i.sstatic.net/KLGzl.png I'm currently attempting to invoke a function from the EmailController inside ITRequests/Scripts/Edit.js, but it's u ...

Have you ever wondered why a listener on the 'data' event interprets two separate requests as one, unless a timeout is added?

It seems that there is a tricky issue with node where it combines 2 different requests into one unless a timeout is added. In the code snippet below, if we write 'one' and 'two' to the server and push the result to an array, node interp ...

Activate an event on a shadow DOM element that is covered by an element in the light DOM (not directly related)

I am currently facing a challenge in retrieving the coordinates of a table cell within a shadow DOM. This table cell may have overlay elements displayed above it, which could span over multiple cells and may not necessarily be direct children of the cell I ...