material icon indicating copy to clipboard operation
material copied to clipboard

feat(dialog): support using AngularJS 1.5+ components directly

Open knvpk opened this issue 10 years ago • 21 comments

Actual behavior:

  • What is the issue? * $mdDialog service is not working with angular 1.5 components.
      $mdDialog.show({
                    template: "<task-show  task="task"></task-show>",
                    autoWrap: false,
                    parent: angular.element(document.body),
                    targetEvent: ev,
                    openFrom: {
                        left: 2500,
                        top: 0,
                        width: 50,
                        height: 500
                    },
                    locals:{
                        task:task
                    },
                    closeTo: {
                        left: 2500,
                        top: 0,
                        width: 50,
                        height: 500
                    },
                    resolve: {
                        module: ['$q', function ($q) {

                            $ocLazyLoad.load({
                                name: module.name
                            });
                        }]
                    }
                }).then(function (message) {

                }, function (cancelmessage) {
                    //console.log(cancelmessage);
                });

May the problem with template, it is not accepting the task as input. which is binding for taskShow component

  • What is the expected behavior? It should open an dialog with given component template

CodePen or Steps to reproduce the issue: *

  • CodePen Demo which shows your issue :
  • Details:

Angular Versions: *

  • Angular Version: 1.5.5
  • Angular Material Version: 1.0.6

Additional Information:

  • Browser Type: chrome
  • Browser Version: 50
  • OS: ubuntu
  • Stack Traces: it clear that task in the input template not coming to controller.

Do not modify the titles or questions. Simply add your responses to the ends of the questions. Add more lines if needed.

knvpk avatar May 11 '16 11:05 knvpk

Everything works for me perfectly.

  • http://codepen.io/DevVersion/pen/wGOPgz?editors=1010

Please modify the codepen to represent your issue.

I guess, your dialog is not opening, because you set autoWrap to false.

devversion avatar May 11 '16 12:05 devversion

Hi @DevVersion , From your demo code i understand that i want to pass the scope:$scope without that it throws some error. but when im trying to send oinly one scope object rather than full $scope it is throwing the error scope.$new is not a function

scope:{
   textInput:$scope.textInput
}

knvpk avatar May 11 '16 14:05 knvpk

@pavankumarkatakam That's true. It seems like our compiler does not support this concept.

I can see the relevance and think it would be a good enhancement for the $$interimElement factory.

devversion avatar May 11 '16 18:05 devversion

A related point, it would be nicer if there was a property where you could set the name of the component, rather than writing out the whole template in the template property. You can do this in ui-router 1.0.0-alpha.4. It would come in handy when you want to programmatically set the component, otherwise you will need to fiddle with strings etc.

$mdDialog.show({
    component: 'taskShow',
    parent: $rootElement,
    scope: $scope,
    preserveScope: true,
    clickOutsideToClose: true
});

jdddog avatar May 21 '16 05:05 jdddog

I think this demo is broken. I am running to this issue today.

Shaunwei avatar Sep 26 '16 00:09 Shaunwei

@Shaunwei the demo just worked for me. I'd also add a note, that, if you don't want to pollute the current scope you can create an isolate scope instead:

ctrl.openItem = function(item, $event){
  // not polluting current $scope...
  var scope = angular.extend($scope.$new(true), {
    item: item
  });

  $mdDialog.show({
    template: '<test item="item"></test>',
    scope: scope
  });
};

joaovieira avatar Oct 27 '16 10:10 joaovieira

@joaovieira From your example I wasn't able to bind the data to the component. However I was able to access the data in the dialog controller by using $scope.$parent.item

AntonisFK avatar Nov 15 '16 19:11 AntonisFK

http://codepen.io/anon/pen/RoGNmm

joaovieira avatar Nov 15 '16 21:11 joaovieira

If the component uses templateUrl (vs. template), a blank dialog is opened, because the opacity is zero (on md-dialog element) as if the animation is not ended.

Maybe a race condition between the animation and the loading of the template.

Edit: http://plnkr.co/edit/GceMCA99eY9ZaTKSEKl6?p=preview Edit2: without <md-dialog> element, wrapping the dialog, the content is displayed (with a few warnings)

albertosantini avatar Nov 26 '16 18:11 albertosantini

I'm experiencing the same issue reported by @albertosantini...

ronyvieira avatar Dec 02 '16 20:12 ronyvieira

For possible reference, this functionality was added to UI Bootstrap a few months ago - https://github.com/angular-ui/bootstrap/pull/6179

T89L avatar Dec 08 '16 18:12 T89L

This is how I pass data to the compoment:

var self = this;
this.$mdDialog.show({
		template: '<task task="$ctrl.task"></task>',
		controller: function () {
			this.ask= self.task;
		},
		controllerAs: '$ctrl',
		parent: angular.element(document.body),
		targetEvent: $event,
		clickOutsideToClose: true,
	})

paddyfink avatar Jan 21 '17 21:01 paddyfink

I have the same problem as @albertosantini and @ronyvieira

Thanks to @albertosantini, this is my current workaround:

// 'Parent'
$mdDialog.show({
  parent: angular.element(document.body),
  targetEvent: $event,
  template: '<md-dialog aria-label="MyLabel"><my-custom-component></my-custom-component></md-dialog>'
})

// my-custom-component.js
//..
// (you could also pass these functions through the template with one-way binding '&')
controller: function ($mdDialog) {
   this.hide = $mdDialog.hide;
   this.show = $mdDialog.show;
}
//..

david-err avatar Apr 09 '17 10:04 david-err

+1. It's a shame that there is no .component syntax (mentioned by @jdddog) in angular-material, like in ui-bootstrap.

Upd. Few minutes later, I realised that current implementation is very proper! User should not mess with caller scope (where dialog was called from), until he or she clicks OK (apply) or cancel buttons! Very user-friendly indeed. Like in traditional desktop OS interfaces. I mean, with .component syntax you may easily use two-side binding, while with current implementation you are forced to do things right, or it is hard to not to

anurbol avatar May 10 '17 01:05 anurbol

Just to clarify @dremie 's workaround, he is (presumably) taking the <md-dialog> out of the custom component and into the template, so the <md-dialog> in the template isn't an extra one, its the one that was originally in the custom component. Doing this resolves an issue I was having with the dialog not getting scrollbars.

colmben avatar May 18 '17 14:05 colmben

I may take a stab at this sometime in the near future

codymikol avatar Feb 16 '19 00:02 codymikol

@codymikol that would be great. Please let me know how I can help 😄

Splaktar avatar Jun 04 '19 00:06 Splaktar

Is there any movement to directly support "component" as a property of the show() like they have had in bootstrap angularjs since 1.5?

ewahner avatar Feb 10 '20 16:02 ewahner

@ewahner nothing more than you can see here in this issue. A PR for this would be welcomed and I can provide some support to the author.

Splaktar avatar Feb 10 '20 23:02 Splaktar