material icon indicating copy to clipboard operation
material copied to clipboard

ng-model tightly bound to click, should provide a method to revert the value

Open soyiatgit opened this issue 11 years ago • 8 comments

I am poping up a dialog box on clicking one of the options of md-select. As soons as the option clicks, the ng-model changes to clicked value. However if the $mdDialog box is cancelled or closed (i.e user did not make any use of dialog box) the ng-model should revert back to its original value.

As I am facing currently the clicked value remain stuck to ng-model. Even if I am changing it again by reassigning the value while rejecting the promise, the value doesn't change on UI.

I think, the value is too tightly coupled and after closing the dialog Angular material again assigns the clicked value to ng-model, which is an issue for my case.

soyiatgit avatar Sep 09 '15 11:09 soyiatgit

Are you saying you're passing in a value to your $mdDialog box via the locals/resolve, modifying the value and it's keeping the change whether they click accept or cancel the dialog?

You'll need to do a bit more logic and you'll find this will happen in Angular (well javascript in general). Make a copy of your variable angular.copy(myVar); then on submit/resolve of the dialog, send the new value, check on your resolve and re assign it to update the UI, if they cancel it, promise is rejected, nothing assigned.

bolerodan avatar Sep 09 '15 12:09 bolerodan

Without a sample, my guess is you're trying to use "this.someModel" to reassign your original value, which will not work. You either need to keep a context/ controller reference (i.e.: var self = this, self.someModel = originalValue) before you enter the promise, or assign the model to the scope like in this example:

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

paulflo150 avatar Sep 09 '15 12:09 paulflo150

I am passing variables in locals, but that variable shud only be used wen the promise is resolved by $hide(). In case, its rejected by $cancel(), the same previous state should be retained. However, as i see, as soon as someone clicks an option from dropdown, the new value or clicked value is shown in ng-model (I guess angular material ties it to ng-model). But when the $cancel() is done, the entire $scope of my Dialog Controller (dialog template is shown by other controller but after $cancel or $hide, promise is resolved in original controller) should end and whatever value was bind to ng-model of md-select before choosing new value, that should again be shown

soyiatgit avatar Sep 09 '15 17:09 soyiatgit

@paulflo150 your codepen is fine and actually what I want, but the only concern is that currently the Dialog part, I am handling in a separate controller. This separate controller is invoked from ctrl-1 in mdDialog.show() and the promise again is resolved in 1st controller. Also the original ng-model for md-select is in ctrl-1 and it is initialised in ctrl-1. Only the pop up part I have in ctrl-2. Any suggestion, what might be going wrong here, because at the time of rejecting promise, I am surely this kind of thing as in codepen $scope.userState = originalValue;

soyiatgit avatar Sep 09 '15 17:09 soyiatgit

This has nothing to do with "ng-model". This has to do with javascript specifically and passing values by reference you are changing. In your controller (for your dialog), make a copy of the variable passed in (the one you are passing in via locals) so it is no longer a reference to the variable in your "parent" controller. Send the new updated value on resolve, then update the value appropriately in your parent controller. If this is the problem what I'm thinking, however I am having a hard time following you, sorry.

Perhaps put together a codepen to confirm what we think you are having issues with.

bolerodan avatar Sep 09 '15 17:09 bolerodan

ya sure..I can understand how difficult it is without actual code before eyes. I will take few tips from above comments and if things still go the same, I will provide a codepen. Hope you will still help :) Thanks for replies

soyiatgit avatar Sep 09 '15 17:09 soyiatgit

Hi @paulflo150 Can you please have a look at http://stackoverflow.com/questions/32475555/md-select-retaining-the-clicked-value

soyiatgit avatar Sep 16 '15 11:09 soyiatgit

Like @bolerodan suggested, you should provide a codepen, otherwise it's hard to tell what your code looks like. The stackoverflow snippets seem incomplete as there is no y that I can see, or items, but here is another attempt based on what I could gather from your comments:

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

paulflo150 avatar Sep 16 '15 13:09 paulflo150