Having trouble assigning a property to a controller within an Angular Material modal dialog

My current setup involves Angular 1.5, ES6, and webpack. Here's a snippet of my code:

export default class HomeController {
  static $inject = ['$mdDialog', '$sce'];

  constructor($mdDialog, $sce) {
    this.$mdDialog = $mdDialog;
    this.tasks = [
      {
        label: "<strong>Something strong</strong>",
        id: 10
      }
    ];
    this.tasks = this.tasks.map(function(item) {
        item.label = $sce.trustAsHtml(item.label);
        return item;
    });
  }

  showRejectionDialog(ev, $index) {
    this.task = this.tasks[$index];
    this.index = $index;
    console.log(this.task);
    console.log(this.index);
    this.$mdDialog.show({
      controller: HomeController,
      preserveScope: true,
      controllerAs: 'vm',
      template: require('./rejectionDialogModal.jade'),
      parent: angular.element(document.body),
      targetEvent: ev,
    });        
  }
  allow($index) {
    this.tasks[$index].status = 'CONFIRMED';
  }
  reject(index) {
    this.$mdDialog.hide();
    console.log(index);
    console.log(this.task);
    console.log(this.index);
    //this.tasks[this.index].status = 'REJECTED';
  }
  cancel() {
      this.$mdDialog.cancel();
  }
}

The content of rejectionDialogModal.jade is as follows:

md-dialog(aria-label='Reject', ng-cloak='')
  form(name="rejectionForm")
    md-dialog-content
      .md-dialog-content
        h2.md-title Reject confirmation for
        div {{vm.task|json}}
        span(ng-bind-html="vm.task.label")
        textarea(placeholder="Please provide a reason for rejection", style="width: 530px; height: 75px")
    md-dialog-actions(layout="row")
      span(flex)
      md-button(ng-click="vm.cancel()") CANCEL
      md-button.md-primary.md-raised(ng-click="vm.reject(vm.index)") REJECT

While the console.log inside showRejectionDialog displays the correct values, the one from reject shows undefined. Additionally, vm.task is undefined inside the dialog and the label is not displayed. How can I properly pass properties to the modal dialog when using the same controller?

Answer №1

Here's a suggestion for creating a controller function with access to the $scope:

showRejectionDialog(ev, $index) {
    this.task = this.tasks[$index];
    this.index = $index;
    console.log(this.task);
    console.log(this.index);
    this.$mdDialog.show({
        controller: function () {
             this.parent = $scope;
        },
      preserveScope: true,
      controllerAs: 'vm',
      template: require('./rejectionDialogModal.jade'),
      parent: angular.element(document.body),
      targetEvent: ev,
    });        
  }

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

Automatically populate select boxes with values from a different source using PHP

I'm in the process of setting up automatic population for 2 select boxes on a website. When a user chooses a class, the second select box automatically displays the main specialization of that class. If the user selects Tank (for example), the third ...

Restrict the number of items in each list to just one

I'm trying to customize a query that displays a list of numbers. My goal is to only display each unique number once. For example, if there are three instances of the number 18 in the database, I want it to show as 18, 18, 18. If there are two occurre ...

I'm currently experiencing a challenge with a project I'm tackling that specifically deals with chart.js

In my recent coding project, I created a script to gather user input and then present it in various chart formats. However, upon executing the code, the selected chart fails to display after inputting values and clicking on the "generate chart" button. Her ...

How can you trigger a link click event when clicking anywhere on the page using Jquery?

Here's the code I'm working with: <a href="http://google.com" target="_blank">Open in new tab </a> I am trying to make it so that when a user clicks anywhere on the website, the link above will be automatically clicked and a new tab ...

What is the best method for enclosing all text within an HTML document with a different element?

For example: FROM <div> whats up! <div> whats up2! </div> thank you for your help <div></div> </div> TO <div> <span>whats up!</span> <div><span> whats up2! </span&g ...

Having trouble converting a name to binary? Clicking on JS isn't producing any results? Don't worry,

Just starting out as an absolute beginner with programming and watching some tutorial videos. I've got VSC installed, but it's not displaying any errors when I run it. Any suggestions for a better program than VSC? I'm trying to convert Inp ...

The function is not being called by ngModelController $parsers in Angular version 1.4.7

I'm encountering an issue with ngModel.NgModelController.$parsers. Let me explain with an example: I have created a directive for an input field. Whenever I change the value in this input in the HTML, it should call my specified function. The registe ...

Obtaining service data results in Angular: A step-by-step guide

To retrieve data from an API, I implemented a controller and service. The service code looks like this: var categories = []; var categoriesjon = function () { $http.get('http://localhost:18737/Category/GetCategories'). success(funct ...

Create a function in JavaScript that is able to accept a variable number of objects as arguments

I have a good grasp of how to pass infinite parameters in a function in JavaScript. But what about accepting any number of objects as parameters in a function? This is my current implementation: function merge<T>(objA: T, objB: T){ return Object. ...

Is there a way to access the history of Vue routers?

I am looking for a way to determine if the Vue router has additional entries in its history that can be navigated back to. This information is crucial for deciding whether or not to execute the exit app function. The app should only navigate back to prev ...

Learn the process of utilizing signed URLs in conjunction with ReactJS to securely store files in AWS

I am currently working on a form where I need to attach images along with regular text inputs in order to submit data to my MongoDB. Here is the code for my post creation function: const [postData, setPostData] = useState({ text: '', i ...

Navigate through pages using scrollspy without losing your position when returning

Hey all you web geeks out there, I could really use your help solving a little issue. I've been working with Bootstrap to build a website that utilizes scrollspy for navigating different sections of the page using the navbar. The only way I could sto ...

error encountered when working with date arrays in JavaScript

I find myself perplexed by this particular issue. Although the following code snippet appears to function correctly, it exhibits some peculiar behavior. var tmpcurdte = eval(dataSource[i].startDate); tmpcurdte.setDate(tmpcurdte.getDate() + 1); while (tm ...

Property finally is missing in the Response type declaration, making it unassignable to type Promise<any>

After removing the async function, I encountered an error stating that the Promise property finally is missing when changing from an async function to a regular function. Any thoughts on why this would happen? handler.ts export class AccountBalanceHandle ...

assistance required (javascript and jquery timer function)

Whenever the button is clicked, a timer of 2 minutes starts. If the button is clicked once and the 2-minute timer completes before another click, everything works perfectly. However, if the user clicks the button before the 2 minutes are up, the timer rese ...

Managing configuration variables in ExpressJS for various environments

Is it possible to set a variable for different environments when defining the environment? app.configure 'development', () -> app.use express.errorHandler({dumpExceptions: true, showStack: true}) mongoose.connect 'mongodb://xxx:<a h ...

Warning: Conversion error encountered while converting array from PHP to JavaScript, please take note

I've been working on extracting arrays of latitudes and longitudes from the PHP variables $lat and $long, which are retrieved from a database. <?php ... $lat[$latlongindex] = $results_row['latitude']; $long[$latlongindex] = $results_ro ...

Utilizing an iPad to control and modify the content displayed on an external monitor

I am uncertain about the exact scope of this project and would appreciate input from those with prior experience. The project involves creating a mobile site that will act as a framework for displaying content. The 'app' will feature a simple li ...

Create JSX code for rendering components outside of the main component

As someone who is diving into the world of React, I am currently on lecture 23 out of 247 on Udemy where I am learning about states and events. However, one particular question has been lingering in my mind without a definitive answer. Our company has mad ...

How to create collapsible and expandable HTML table columns using the <th> element?

I've searched far and wide for a solution to this conundrum and come up empty-handed. I have a HTML table with a total of 13 columns. The 7th column, labeled Number of Tops, is a sum of columns 8-11: # Short-sleeve, # Long-sleeve, # Sweaters, # Jacket ...