The $mdDialog component from Angular Material fails to display again after being hidden once

There seems to be an issue with my app's chat button functionality. When the chat button is clicked, it opens an $mdDialog. The problem arises when I use $mdDialog.hide() to close the dialog window after clicking on a user name within the dialog. Subsequently, clicking on the chat button again does not work as expected.

If anyone has any advice or solutions, I would greatly appreciate it. You can view my code on CodePen.

HTML

<div ng-app="MyApp" ng-controller="AppCtrl">
  <div id="menubar">
    <div class="logo"><a href="#"><img src="http://i.imgur.com/yS9Ug9Z.png"/></a></div>
    ...

JS

angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])

.controller('AppCtrl', function($scope, $mdDialog) {
  //CHAT
  ...
})
// menubar functions
...
}

/**
 * menubar behavior
 */

CSS

html, body {
  padding: 0;
  ...

Answer №1

After looking into your codepen and experimenting with it, I discovered that the issue is resolved when you remove the line "scope: $scope". It seems that by following the example from the documentation on $ngDialog, which includes adding the option "preserveScope: true," you can achieve the desired outcome.

// Dialog #3 - Demonstrate use of ControllerAs and passing $scope to dialog
//             Here we used ng-controller="GreetingController as vm" and
//             $scope.vm === <controller instance="">
function showCustomGreeting() {
   $mdDialog.show({
      clickOutsideToClose: true,
      scope: $scope,        // use parent scope in template
      preserveScope: true,  // do not forget this if use parent scope
      // Since GreetingController is instantiated with ControllerAs syntax
      // AND we are passing the parent '$scope' to the dialog, we MUST
      // use 'vm.<xxx>' in the template markup
      template: '<md-dialog>' +
                '  <md-dialog-content>' +
                '     Hi There {{vm.employee}}' +
                '  </md-dialog-content>' +
                '</md-dialog>',
      controller: function DialogController($scope, $mdDialog) {
        $scope.closeDialog = function() {
          $mdDialog.hide();
        }
      }
   });
}

For more information about the scope options, refer to the documentation excerpt below.

scope - {object=}: the scope to link the template / controller to. If none is specified, it will create a new isolate scope. This scope will be destroyed when the dialog is removed unless preserveScope is set to true.

This approach can also be applied to your chatDialog. Please view the updated codepen for reference.


You are currently using the 'AppCtrl' controller for your dialog, which causes the controller to be re-instantiated inside the dialog. To rectify this, make sure your code resembles the following snippet.

$scope.openChatDialog = function() {
    $mdDialog.show({
      scope: $scope,
      preserveScope: true,
      template: '<md-button ng-click="openChat(\'everybody\')">Everybody</md-button><md-button ng-repeat="user in collaborators" ng-click="openChat(user)"><svg class="status-light" height="17" width="17"><circle cx="8" cy="8" r="8" fill="lightGreen" /></svg>{{user}}</md-button>',
      hasBackdrop: false,
      clickOutsideToClose: true,
      openFrom: '#chat-btn',
      closeTo: '#chat-btn'
    })
  };

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

Create an image on a node's backdrop using a library of graph theory/networking techniques

I have a set of data that I need to visually represent as a graph on a web browser. While creating the graph itself is not an issue, I am looking to dynamically draw unique icons for each node. These icons are specific to the characteristics of each node ...

Managing automatic page redirects in Ajax requests

For the past two days, I have been encountering a challenging issue with my Laravel application that is hosted on Heroku server. The app allows file uploads through an ajax request, displaying upload progress on the page and returning JSON response upon co ...

Ensure that text is aligned alongside an image only when both elements are enclosed within <p> tags

When using an API to pull markdown content from Contentful and converting it to HTML with ReactMarkdown, a common issue arises. The resulting HTML structure often places the text content in separate paragraphs such as: <p>Some text content</p> ...

Executing a controller method in Grails using JavaScript

When working in a Grails view, I find myself needing to execute a JavaScript method to retrieve certain information. To achieve this, I have set up a submit action as shown below: <input type="submit" name="submit" class="submit action-button" value="G ...

Using a Vue.js component in a Laravel Blade template can be achieved by first registering the component

I added a component registration in my app.js as shown below: Vue.component('navbar', require('./components/Navbar.vue')); Now I am looking to import that component into my blade.php file like so: <template> <nav class="navb ...

Top method for transferring the result of a function to descendant components in Vue

In my parent component, I have a data object named config structured like this: data() { return { config: { Groups: [ { name: "A", Types: [ { mask: 1234, name: ...

Tips for retrieving text within a Div and button element

<div class="content-wrapper" style="min-height: 611px;"> <div class="alert alert-success alert-dismissable"> <button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button> The addition of the new c ...

Is your Ajax response suddenly failing to work after the initial attempt?

Describing my predicament: The code snippet below is what I have been using to insert a custom-designed div into my webpage. Initially, the div is successfully added; however, it stops working after the first instance. $('#addanother').click(fu ...

Obtain and send a variety of options for selection button values

I want to create a range selector using submit buttons on my website. In page1.php, I have an array called $array which contains years like 1980, 1990, 2000, 2010, .... The range of this array is updated based on database content. <form method="pos ...

A step-by-step guide to adding a checkbox column dynamically within handsontable

I am currently utilizing handsontable within a jsfiddle at http://jsfiddle.net/kc11/cb920ear/1/. My task involves dynamically inserting a checkbox column before the existing data. The structure I am working with appears to be a multidimensional array, as s ...

Tips on transforming a grouped object into a table organized by column with the help of Lodash

Looking at my array data: [{ id: '1234', year: 2019 , name: 'Test 1- 2019', rate: 1}, { id: '1234', year: 2020, name: 'Test 2 - 2020', rate: 2 }, { id: '1234', year: 2020, name: 'Test 3 - 2020&apos ...

What is the best way to create dynamic transparency based on cursor position?

Is there a way to create an animation like the one on https://meetwalter.com, where the transparency changes with the cursor's position? I previously attempted a similar implementation using CSS, but it seems that the website accomplishes this effect ...

Leveraging webpack in conjunction with Angular 1.5, PHP, and Bower

Here is the structure of my index.php file: <script src="app/bower_components/jquery/dist/jquery.min.js"></script> <script src="app/bower_components/angular/angular.min.js"></script> <script src="app/bower_components/angular-ui- ...

Guide on invoking a Python function using vanilla JavaScript within a Django application

I'm currently developing a Django application and I'm attempting to invoke a Python function in views.py upon clicking a button. I'm aiming to achieve this using vanilla JavaScript, as my knowledge of JavaScript is limited and I prefer to ke ...

Utilizing JavaScript to trigger a series of click events on a single button in order to

I am working on implementing a click event for a checkbox that will add and remove CSS classes using the "classList.toggle" method. Specifically, I want the checkbox to toggle between adding the "xyz" class on the first click, and then adding the "abc" cla ...

locating the image file path in an Angular ng file upload

Once I've uploaded an image using ng-file-upload to a server like https://angular-file-upload-cors-srv.appspot.com/upload, all I receive in response is the image name. Now, in order to display the image in HTML, how can I achieve this? <img ng-sh ...

Merge corresponding elements from two arrays based on their indices

If I have two arrays of objects structured like this: var arr1 = [{name: 'Jay'}, {name: 'Bob'}]; var arr2 = [{age: 22}, {age: 30}]; I am looking to merge them into a combined array as follows: var arr3 = [{name: 'jay', age: ...

Centering a table horizontally using CSS when its position is set to absolute

I currently have 5 HTML pages with identical code. The code snippet looks like this: <div id='generic'> </div> All 5 pages are linked to an external CSS file that contains the following styling: <style> #generic { ...

Modifying Data in Another Component in VueJS

I have a classic Vue Component structured like the following: Vue.component('bar', { template: `<div class="bar"></div>`, data () { return { blocks: [ ] ...

Acquire JSON data from a URL and display it on the webpage

I'm facing an issue where the JSON data I'm trying to fetch from a URL is not displaying due to an uncaught reference error in my code. How can I modify the code to ensure that the data gets shown? var url = "https://fantasy.premierleague.com/ ...