Incorporating an HTML page into a dialog using AngularJS: A step-by-step guide

Is it possible to dynamically change the content of a dialog by updating the URL? I have multiple HTML pages that I want to display in my dialog by simply changing the URL.

<md-button ng-click="showDialog($event,'myurl/page-1.html')">Show dialog-1</md-button>
<md-button ng-click="showDialog($event,'myurl/page-2.html')">Show dialog-2</md-button>
<md-button ng-click="showDialog($event,'myurl/page-3.html')">Show dialog-3</md-button>

My attempted solution:

JavaScript/AngularJS:

(function () {
    angular.module('myApp', ['ngMaterial']).controller('AppCtrl', AppCtrl);
    function AppCtrl($mdDialog, $scope) {
         $scope.data = {url: 'Hello World'}; // "myurl/page.html" <--- I want to add external page here
         $scope.hideDialog = $mdDialog.hide;
         $scope.showDialog = showDialog;

         function showDialog(evt) {
              $scope.data.dialogOpen = true;
              $mdDialog.show({
                   targetEvent: evt,
                   scope: $scope.$new(),
                   template:
                   '<md-dialog>' +
                   '<md-toolbar>' + 
                   '  <div class="md-toolbar-tools"><h2>Mango (Fruit)</h2></div>' + 
                   '</md-toolbar>' + 
                   '  <md-content>{{data.url}}</md-content>' +
                   '  <div class="md-actions">' +
                   '    <md-button ng-click="data.dialogOpen=false;hideDialog()">' +
                   '      Close' +
                   '    </md-button>' +
                   '  </div>' +
                   '</md-dialog>'
              });
         }
    }
}());

Plunker

I attempted to assign the path of my page to $scope.data, but I am aware that this approach will not work. Any suggestions on how I can add the path to external content in an AngularJS dialog?

Answer №1

If you're looking to handle routes in your Angular application, you have a couple of options such as using ngRoute or ui-router. Here's an example using ngRoute:

App.config(function($routeProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl : 'pages/home.html',
            controller  : 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl : 'pages/about.html',
            controller  : 'aboutController'
        })

        // route for the contact page
        .when('/contact', {
            templateUrl : 'pages/contact.html',
            controller  : 'contactController'
        });
});

Answer №2

If you're looking to incorporate an external file as the content of your dialog, consider using the templateUrl attribute when displaying the dialog instead of template. For more options on configuring your dialog, refer to the following link: https://material.angularjs.org/0.9.6/#/api/material.components.dialog/service/$mdDialog

I have adapted your plunkr to include the necessary changes for loading dialog content from external files. Feel free to check out this functional demo:

http://plnkr.co/edit/WHQko1l4p7xHQx0dz5wh?p=preview

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

Communication between a directive controller and a service via an HTTP call

I'm currently developing an Angular directive that loads a Highchart.js area graph by passing some variables to it. Here's how I am using the directive: <andamento-fondo-area-chart color="#3FAE2A" url="../data.json"></andamento-fondo-a ...

What are some effective ways to manage MySQL queries using asynchronous functions in JavaScript?

Can anyone assist me with my issue? I am new to JavaScript (Node.js) and attempting MySQL queries, but it seems like nothing is being output. I'm unsure how to address this. async function processArguments() { var result_customer = []; for(le ...

Determine the number of times a user has accessed a page on my website

Is there a way to track how many times a user loads pages on my website using JavaScript? Could it be done like this? var pageLoads = 1; $(document).ready(function(){ var pageLoads ++; }); Should I store this information in a cookie? ...

Angular JS is throwing an error because it cannot recognize the property 'push' of undefined

Would like to automatically update the div using $scope.push encountering an issue: Error: Cannot read property 'push' of undefined Here are my JSON and JavaScript snippets: JSON {"records":[{"total":"156000"}]} JavaScript $scope.plusCar ...

Adding a CSS class to a Vue component with a custom prop

I am looking to create a custom component in Vue that has the following props: text = the text to display in the link. icon = the identifier of the icon to display next to the link. < sidebar-link text="Home" icon="fa-home"> Below is the .Vue ...

Unable to display Three.JS OBJ Model

I am facing an issue with loading a .obj model in Three.js. I created the model in Cinema 4D, exported it with a scale of 1 meter, and tried to load it using OBJLoader in Three.js. However, even though there are no errors, the model is not showing up. Wh ...

Conquering cross-origin resource sharing (CORS) using XMLHttpRequest without relying on JSONP

Appreciate your time in reading this inquiry! For the past few days, I've been grappling with an AJAX request issue. Despite scouring through numerous answers on Stack Overflow, I'm still unable to find a resolution. Any assistance would be grea ...

Determine the total value derived from adding two option values together

I am attempting to calculate the sum of two select options and display the result. So far, my attempts have only resulted in returning the value 1. What I am aiming for is to add the values from n_adult and n_children, and then show the total under Numbe ...

Working with an array of object in Vuex for form handling

Looking to make updates to a vuex store that includes an array of objects: Users have a combobox for making selections, which then updates a property of the object in the database. However, every time I choose an item from the autocomplete (combobox) I e ...

What is the best way to use Promise.all() to delay the return of an array that is currently being constructed within the function?

In the following code block, I am trying to set an array as the value for a local variable by calling a function that returns an array. The issue arises because the Promises in the second function are not resolved until after the array has been returned to ...

Implementing a Where Condition in JavaScript with the MongoDB whereObj

Working on a project involving JavaScript and MongoDB has led me to a specific collection named test_collection. Within this collection, there is a field/object called test_field_1 which contains test_sub_field_1 and test_sub_field_2. Currently, I am sett ...

Examining the execution of a function/method when a click event occurs within a functional component

It took me a while to realize that testing functional components with vue-test-utils can present some challenges In my case, I am utilizing Bootstrap-Vue's B-Button with a @click event that calls a function/method. When I attempt to test whether the ...

Combining the Angular Material Design mdToolbar with the Ionic ion-nav-bar: A Match Made

Currently, I am in the process of creating an application using Ionic and Angular Material. However, I am facing a challenge with getting Ionics back button to function properly when it is used within the mdToolbar directive of Angular Material. To illust ...

Encounter TypeScript error when using getFirebaseAdmin from next-firebase-auth

I am currently utilizing a package called next-firebase-auth. I am encountering an issue when calling getFirebaseAdmin from next-firebase-auth and attempting to use it, specifically when using db as a reference in doc(db). The error message received is: ...

Is there a way to modify the window's location without having to reload it and without resorting to any sne

Initially, I believed that the hash hack was a necessity, but after observing the recent updates from Facebook, my perspective has shifted. The original hash hack (not certain if this is the correct term) involved changing location.hash to save a state in ...

Changing a button's text in Vue.js when holding a keyboard modifier - how to do it!

While working with Vue, I am attempting to modify a button's behavior when the Shift key is pressed. Adjusting the behavior of the click event was straightforward: @click.exact="goForward" @click.shift="goBackward" However, I am f ...

Is the JQuery Mobile .page() method triggering an endless loop?

Creating a dynamic listview with data from an AJAX response has been successful, however, when using JQM's .page() function on it, it appears to enter an infinite loop where the listview keeps getting appended indefinitely. I'm unsure if this is ...

Displaying Vue.js tooltips in a table when the text gets clipped

I'm currently facing an issue with creating a tooltip in my vue.js document. I attempted to follow this guide from https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_tooltip in order to create one, but it's not working as expected. Her ...

Is it feasible to obtain multiple tag-name indexes using JavaScript?

Exploring the table search function provided by W3Schools has brought up an interesting question in my mind. Is it feasible to simultaneously retrieve multiple indexes using getElementsByTagName and conduct a search across the entire table instead of just ...

Guide to implementing a personalized filter in AngularJS 1.6

I am struggling with injecting a custom filter, status, into my component. Below is the code for my component: function ClaimsListController(dpClaimsListService) { var ctrl = this; ctrl.claims = null; ctrl.searchCriterion = null; ctrl.l ...