Transforming the exterior designs in Angular

I am in the process of transitioning a product management admin site to Angular. Currently, all my views (such as the product list, detail view, and edit) are displayed inside an ng-view on my admin page, which is working well. However, I have a link for each product that allows me to print its information, and this currently uses a different outer template than the others.

What is the best way to handle this situation in Angular?

Here is the code snippet:

angular.module('myApp', []).
  config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

    // All routes except 'print' use an index.html template
    $routeProvider.
      when('/', {
        templateUrl: '/partials/order/manage.html',
        controller: ManageOrderCtrl
      }).
      when('/order/:id', {
       templateUrl: '/partials/order/_view.html',
        controller: ViewOrderCtrl
      }).
      when('/order/edit/:id', {
       templateUrl: '/partials/order/_edit.html',
        controller: ViewOrderCtrl
      }).
      // For printing order info - custom headers and a separate template (_print.html)
      when('/order/print/:id', {
       templateUrl: '/partials/order/_print.html',
        controller: PrintOrderCtrl
      }).
      otherwise({
        redirectTo: '/'
      });
    $locationProvider.html5Mode(true);
  }]);

In my list of orders:

<div ng-repeat="order in orders">
  <a title="Print product sheet" href="/order/print/{{ order._id }}"></a>
</div>

Currently, the _print.html template is loaded within the same ng-view as the other templates. Is there a way to open it in a new window or should I create a new App for this purpose?

Answer №1

To fetch HTML content using AJAX, you can create a service that makes the call to retrieve the HTML. Within this HTML content, placeholders like {{}} can be used for data substitution. To replace these placeholders with actual data, you will need to utilize a template library such as Mustache.

factory('print', ["$window", function($window) {
/* Service responsible for printing */
return function(templateUrl, context) {
    /* Make a GET request to retrieve the template from templateUrl, render it with the given context,
     * and display the content in a new window.
     */
    $.ajax({
        url: templateUrl,
        async: false,   // To prevent popup blocking
        success: function(html) {
            var template = html.replace('<!DOCTYPE html>\n<html lang="en">\n', '').replace("</html>", ''),
                output = Mustache.render(template, context);
            $window.open().document.documentElement.innerHTML = output;
        }
    });
};

}]);

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

TypeScript enabled npm package

I am currently developing a npm module using TypeScript. Within my library, I have the following directory structure: . ├── README.md ├── dist │ ├── index.d.ts │ └── index.js ├── lib │ └── index.ts ├── ...

Guide to setting up an object array in JavaScript with unique child elements and no duplicate values

I have an array that looks like this: sampleArray = ["x", "y", "z". "a.b", "a.b.c", "a.b.d", "a.e.f", "a.e.g"] However, I am aiming to transform it into the following structure, or something similar to it: newArray: [ { "x": [] }, ...

Laravel route does not receive a parameter sent via Ajax

I am currently using Laravel 5.8 and implementing a discount code system on my website. To achieve this, I attempted to send data via Ajax in the following manner: $.ajax({ type: 'POST', url: baseurl + 'discount/register', ...

The requested function is nowhere to be found within the confines of my Controller module

While working on a personal project, I encountered an issue where a function from another class in a separate Java file is not being found, even though it is defined in the respective class. EventView.js: displayEvent(event){ this.EventTitle = event. ...

php After the ajax request, the array_push function is failing to add values to the

I am having trouble with my ajax and php implementation. I want to append an array every time an ajax call is made, but it doesn't seem to be working. Here are the codes I am using: $('#multiple_upload_form' +count).ajaxForm({ ...

Only displaying sub items upon clicking the parent item in VueJS

I'm in the process of designing a navigation sidebar with main items and corresponding sub-items. I want the sub-item to be visible only when its parent item is clicked, and when a sub-item is clicked, I aim for it to stand out with a different color. ...

Passing JSON data from an ASP.NET controller to a view

My issue arises when a web page is loaded and triggers a controller action to retrieve data based on user selection. I am trying to return this data as a JSON object, but it appears as a single string within the HTML page. The basic structure of the contro ...

Utilize Array in Form Input with Index and Spread Operator

Looking to create a form that can handle array data with dynamic fields in TypeScript. Encountering the following error: Element implicitly has an 'any' type because expression of type 'any' can't be used to index type '{ nam ...

Tips for transforming my JSON format into the necessary column layout for C3.js

The data structure returned by my API is as follows. However, I need to reformat this structure for use in C3.js. { "data":{ "test7":[ { "Date":"2016-04-26 00:00:00", "aId":7, "Amount":436464, "Piece":37 ...

The modifications made in ng-grid do not synchronize with the model

Take a look at this example plunker: http://plnkr.co/edit/9iCNie?p=preview When editing a name, the expected behavior is that myData should change, but unfortunately it does not. HTML: <body ng-controller="MyCtrl"> {{myData}} <div clas ...

Activating the change event in jQuery: A step-by-step guide

Within my ASP.NET page, I have included a UserControl that contains a RadioButtonList. Whenever an item in the RadioButtonList is changed, I need to trigger the following jQuery function: $('#rdlUser').change(function (e) { alert("change fun ...

Updating the output without the need for a page refresh following the insertion of data into the database

I'm interested in enhancing the interactivity of this section in my code. Currently, I utilize a form to send announcements via ajax to a php file which successfully updates my database. The table displays the data effectively. However, I want these c ...

Is there a way to retrieve the list of files from a static public folder using javascript?

I have successfully set up a public folder directory using express and node. For instance, this code works perfectly - var testImage = new Image(); testImage.src = '/images/png/avatar.png'; However, I need to access several images stored ins ...

Encountering a "Raphael is undefined" error message when working with Treant.js

I need help creating an organizational flow chart using treant.js. Below is my code snippet, but I'm encountering a 'Raphael is not defined' error that I can't seem to solve. Can someone please assist me with identifying the root cause ...

Utilizing properties from the same object based on certain conditions

Here's a perplexing query that's been on my mind lately. I have this object with all the styles I need to apply to an element in my React app. const LinkStyle = { textDecoration : 'none', color : 'rgba(58, 62, 65, 1)', ...

Creating a Dynamic Dependent Dropdown with Jquery and Ajax in PHP

As a newbie in coding, I stumbled upon some valuable information to enhance my register form using Ajax. While the PHP files seem to be functioning correctly, I suspect that the JS file is not performing as expected. In the register form, you'll find ...

Browsing through a JSON document

Currently, I am attempting to extract schedule information from a JSON file called js/schedule.json. The format of this file is as follows: [ {"date":"12/06/2014","day":"Thursday","kick-off":"21:00","team1":"Brazil","team2":"Croatia","group":"A","stage":" ...

What is the best way to have text wrap around an icon in my React application?

I am facing an issue while trying to display the note description over the trash icon in a React app. I have tried various methods but can't seem to achieve the desired effect. Can anyone guide me on how to get this layout? Here is what I intend to a ...

Laravel Mix causes errors when running `npm run dev` and breaks the package

My nodejs package is built using ES6 features such as 'let', the spread operator (...) and default values for function arguments. However, when I run npm run production with Laravel Mix, an error message appears: ERROR Failed to compile with ...

Having trouble getting Node.js, express, socket.io, and ejs to work together?

Currently, I am working on improving my knowledge of java-script and had the idea to create a basic chat app using Express, stock.io, and ejs. Unfortunately, I am facing some challenges in getting it up and running smoothly. Below is the snippet from my ...