material icon indicating copy to clipboard operation
material copied to clipboard

locals

Open eugene88888 opened this issue 10 years ago • 3 comments

Actual behavior:

  • What is the issue? * Can't get locals work when creating a simple dialog.
  • What is the expected behavior?

CodePen or Steps to reproduce the issue: *

  • CodePen Demo which shows your issue : I am trying to create a simple dialog and pass a variable through locals. This is the codepen where you can see the variable is not appearing: Demo 1.
  • When I set scope: $scope to the dialog, the variable is appearing but after closing dialog, the button never triggers again. This is the codepen: Demo 2.
  • Details:

Angular Versions: *

  • Angular Version: 1.5.5
  • Angular Material Version: v1.1.0-rc.5

Additional Information:

  • Browser Type: Google Chrome *
  • Browser Version: Version 51.0.2704.106 (64-bit) *
  • OS: Linux Mint 17.3 Rosa *
  • Stack Traces:

Shortcut to create a new CodePen Demo. Note: * indicates required information. Without this information, your issue may be auto-closed.

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

eugene88888 avatar Jun 27 '16 13:06 eugene88888

@Jenny888 By using a prerendered dialog, the {{name}} variable in the html is bound to the originating angular scope, i.e. the scope for AppCtrl. The default action for creating a dialog controller's scope is to make that scope isolate. However, you can use the scope parameter of the $mdDialog options to bind the dialog scope to the AppCtrl scope. See here: http://codepen.io/anon/pen/LZWEje?editors=1010

Do note though that since you're binding the scope of AppCtrl to the DialogController, you're defeating the purpose of locals, which allow you to pass in parameters to an isolated scope in the first place. So, if you want to keep using an isolated scope (recommended) and keep using locals (also recommended), you'll have to use a template for your dialog, like this: http://codepen.io/anon/pen/XKMJep?editors=1010

Hope that helps.

Edit: Just saw the second demo, not sure what's going on there honestly. Suppose my response was hasty then.

Edit2: Okay, figured it out. It's an issue with preserveScope. Since you're binding the scope of AppCtrl into the dialog and the dialog deletes scopes by default, you just need to set preserveScope: true. Demo: http://codepen.io/anon/pen/XKMJZM?editors=1010

Edit3: Damn, @DevVersion beat me to it. Nice job.

zlovin avatar Jun 27 '16 15:06 zlovin

This is not an issue. I'll explain why.

Demo 1 You're using a pre-rendered dialog, which will be not (re)-compiled upon dialog opening. This would mean the dialog would never retrieve it's new scope from the controller. It only uses the scope from your App Controller, since the dialog element was placed there.

One approach to fix that, would be to recompile the pre-rendered dialog itself. But this would actually make the pre-rendered dialog quite unnecessary.

Demo 2 Since you're using the default scope of your main controller and the scope will be destroyed upon dialog close, you have to preserve the scope on destroy.

{
  preserveScope: true,
  scope: $scope
}

Just saw, while I was writing, that @zlovin posted some correct thoughts. Thanks.

devversion avatar Jun 27 '16 15:06 devversion

@zlovin, @DevVersion Thank you very much for the explanation and help. It was very helpful!

eugene88888 avatar Jun 28 '16 06:06 eugene88888