mat-slide-toggle shouldn't change it's state when I click cancel in confirmation window












1















When I checked the slide toggle it should work as expected. But when I try to uncheck it there is a confirmation window asking if you are sure. When I click cancel in that confirmation window, still the toggle changes to unchecked, which shouldn't happen. It should stay in the same state.



Here is my Html and TS Code



// html
<mat-slide-toggle
(change)="change($event)" [checked]="isChecked()" >
To-pay
</mat-slide-toggle>


TS code:



// ts
change(e) {
if(this.checked) {
if(confirm("Are you sure")) {
console.log("toggle")
}
else {
e.stopPropagation();
console.log("toggle should not change if I click the cancel button")
}
}
}


when the confirmation window comes while trying to uncheck the toggle button and I click the cancel option there, nothing should happen to the toggle. stopPropagation() is also not working.



Edit1 :I tried doing return false in else statement. didn't worked. Even tried to change the e.checked = false. It just changed the event object value but didn't prevent the toggle from slidding










share|improve this question

























  • What about a "return false" instead of the stopPropagation ?

    – Leogiciel
    Jan 2 at 11:09











  • I tried it. didn't worked

    – UdG
    Jan 2 at 11:12











  • Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

    – Leogiciel
    Jan 2 at 11:19













  • ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

    – UdG
    Jan 2 at 11:21











  • What version of Angular do you use ?

    – Leogiciel
    Jan 2 at 11:27


















1















When I checked the slide toggle it should work as expected. But when I try to uncheck it there is a confirmation window asking if you are sure. When I click cancel in that confirmation window, still the toggle changes to unchecked, which shouldn't happen. It should stay in the same state.



Here is my Html and TS Code



// html
<mat-slide-toggle
(change)="change($event)" [checked]="isChecked()" >
To-pay
</mat-slide-toggle>


TS code:



// ts
change(e) {
if(this.checked) {
if(confirm("Are you sure")) {
console.log("toggle")
}
else {
e.stopPropagation();
console.log("toggle should not change if I click the cancel button")
}
}
}


when the confirmation window comes while trying to uncheck the toggle button and I click the cancel option there, nothing should happen to the toggle. stopPropagation() is also not working.



Edit1 :I tried doing return false in else statement. didn't worked. Even tried to change the e.checked = false. It just changed the event object value but didn't prevent the toggle from slidding










share|improve this question

























  • What about a "return false" instead of the stopPropagation ?

    – Leogiciel
    Jan 2 at 11:09











  • I tried it. didn't worked

    – UdG
    Jan 2 at 11:12











  • Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

    – Leogiciel
    Jan 2 at 11:19













  • ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

    – UdG
    Jan 2 at 11:21











  • What version of Angular do you use ?

    – Leogiciel
    Jan 2 at 11:27
















1












1








1








When I checked the slide toggle it should work as expected. But when I try to uncheck it there is a confirmation window asking if you are sure. When I click cancel in that confirmation window, still the toggle changes to unchecked, which shouldn't happen. It should stay in the same state.



Here is my Html and TS Code



// html
<mat-slide-toggle
(change)="change($event)" [checked]="isChecked()" >
To-pay
</mat-slide-toggle>


TS code:



// ts
change(e) {
if(this.checked) {
if(confirm("Are you sure")) {
console.log("toggle")
}
else {
e.stopPropagation();
console.log("toggle should not change if I click the cancel button")
}
}
}


when the confirmation window comes while trying to uncheck the toggle button and I click the cancel option there, nothing should happen to the toggle. stopPropagation() is also not working.



Edit1 :I tried doing return false in else statement. didn't worked. Even tried to change the e.checked = false. It just changed the event object value but didn't prevent the toggle from slidding










share|improve this question
















When I checked the slide toggle it should work as expected. But when I try to uncheck it there is a confirmation window asking if you are sure. When I click cancel in that confirmation window, still the toggle changes to unchecked, which shouldn't happen. It should stay in the same state.



Here is my Html and TS Code



// html
<mat-slide-toggle
(change)="change($event)" [checked]="isChecked()" >
To-pay
</mat-slide-toggle>


TS code:



// ts
change(e) {
if(this.checked) {
if(confirm("Are you sure")) {
console.log("toggle")
}
else {
e.stopPropagation();
console.log("toggle should not change if I click the cancel button")
}
}
}


when the confirmation window comes while trying to uncheck the toggle button and I click the cancel option there, nothing should happen to the toggle. stopPropagation() is also not working.



Edit1 :I tried doing return false in else statement. didn't worked. Even tried to change the e.checked = false. It just changed the event object value but didn't prevent the toggle from slidding







angular angular-material angular5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 11:28







UdG

















asked Jan 2 at 11:05









UdGUdG

989




989













  • What about a "return false" instead of the stopPropagation ?

    – Leogiciel
    Jan 2 at 11:09











  • I tried it. didn't worked

    – UdG
    Jan 2 at 11:12











  • Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

    – Leogiciel
    Jan 2 at 11:19













  • ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

    – UdG
    Jan 2 at 11:21











  • What version of Angular do you use ?

    – Leogiciel
    Jan 2 at 11:27





















  • What about a "return false" instead of the stopPropagation ?

    – Leogiciel
    Jan 2 at 11:09











  • I tried it. didn't worked

    – UdG
    Jan 2 at 11:12











  • Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

    – Leogiciel
    Jan 2 at 11:19













  • ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

    – UdG
    Jan 2 at 11:21











  • What version of Angular do you use ?

    – Leogiciel
    Jan 2 at 11:27



















What about a "return false" instead of the stopPropagation ?

– Leogiciel
Jan 2 at 11:09





What about a "return false" instead of the stopPropagation ?

– Leogiciel
Jan 2 at 11:09













I tried it. didn't worked

– UdG
Jan 2 at 11:12





I tried it. didn't worked

– UdG
Jan 2 at 11:12













Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

– Leogiciel
Jan 2 at 11:19







Is "this.checked" true when you pass in this method at uncheck ? Maybe the problem is you have to "else" on the first if and not the second one...

– Leogiciel
Jan 2 at 11:19















ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

– UdG
Jan 2 at 11:21





ya this.checked is true when i try to uncheck. if this.checked is false , it shouldn't enter the code of showing confirm window

– UdG
Jan 2 at 11:21













What version of Angular do you use ?

– Leogiciel
Jan 2 at 11:27







What version of Angular do you use ?

– Leogiciel
Jan 2 at 11:27














2 Answers
2






active

oldest

votes


















1














Unfortunately you cannot use stopPropagation with mat-slide-toggle, you will need to programmatically set the checked value back to true on the event in your else condition.



else {
e.source.checked = true;
console.log("toggle should not change if I click the cancel button")
}


Stackblitz



https://stackblitz.com/edit/angular-79yf7m?embed=1&file=app/slide-toggle-overview-example.ts






share|improve this answer
























  • it Worked. Thanks. "source" was what missing in my trials.

    – UdG
    Jan 3 at 5:52



















0














I think the matter is the (change)="eventHandler()" will fire AFTER the value change, while the (ngModelChange)="eventHandler()" will fire BEFORE the value change






share|improve this answer
























  • ngModelChange is not working in my usecase

    – UdG
    Jan 2 at 11:37











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005182%2fmat-slide-toggle-shouldnt-change-its-state-when-i-click-cancel-in-confirmation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Unfortunately you cannot use stopPropagation with mat-slide-toggle, you will need to programmatically set the checked value back to true on the event in your else condition.



else {
e.source.checked = true;
console.log("toggle should not change if I click the cancel button")
}


Stackblitz



https://stackblitz.com/edit/angular-79yf7m?embed=1&file=app/slide-toggle-overview-example.ts






share|improve this answer
























  • it Worked. Thanks. "source" was what missing in my trials.

    – UdG
    Jan 3 at 5:52
















1














Unfortunately you cannot use stopPropagation with mat-slide-toggle, you will need to programmatically set the checked value back to true on the event in your else condition.



else {
e.source.checked = true;
console.log("toggle should not change if I click the cancel button")
}


Stackblitz



https://stackblitz.com/edit/angular-79yf7m?embed=1&file=app/slide-toggle-overview-example.ts






share|improve this answer
























  • it Worked. Thanks. "source" was what missing in my trials.

    – UdG
    Jan 3 at 5:52














1












1








1







Unfortunately you cannot use stopPropagation with mat-slide-toggle, you will need to programmatically set the checked value back to true on the event in your else condition.



else {
e.source.checked = true;
console.log("toggle should not change if I click the cancel button")
}


Stackblitz



https://stackblitz.com/edit/angular-79yf7m?embed=1&file=app/slide-toggle-overview-example.ts






share|improve this answer













Unfortunately you cannot use stopPropagation with mat-slide-toggle, you will need to programmatically set the checked value back to true on the event in your else condition.



else {
e.source.checked = true;
console.log("toggle should not change if I click the cancel button")
}


Stackblitz



https://stackblitz.com/edit/angular-79yf7m?embed=1&file=app/slide-toggle-overview-example.ts







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:34









MarshalMarshal

3,2722521




3,2722521













  • it Worked. Thanks. "source" was what missing in my trials.

    – UdG
    Jan 3 at 5:52



















  • it Worked. Thanks. "source" was what missing in my trials.

    – UdG
    Jan 3 at 5:52

















it Worked. Thanks. "source" was what missing in my trials.

– UdG
Jan 3 at 5:52





it Worked. Thanks. "source" was what missing in my trials.

– UdG
Jan 3 at 5:52













0














I think the matter is the (change)="eventHandler()" will fire AFTER the value change, while the (ngModelChange)="eventHandler()" will fire BEFORE the value change






share|improve this answer
























  • ngModelChange is not working in my usecase

    – UdG
    Jan 2 at 11:37
















0














I think the matter is the (change)="eventHandler()" will fire AFTER the value change, while the (ngModelChange)="eventHandler()" will fire BEFORE the value change






share|improve this answer
























  • ngModelChange is not working in my usecase

    – UdG
    Jan 2 at 11:37














0












0








0







I think the matter is the (change)="eventHandler()" will fire AFTER the value change, while the (ngModelChange)="eventHandler()" will fire BEFORE the value change






share|improve this answer













I think the matter is the (change)="eventHandler()" will fire AFTER the value change, while the (ngModelChange)="eventHandler()" will fire BEFORE the value change







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 11:33









LeogicielLeogiciel

15810




15810













  • ngModelChange is not working in my usecase

    – UdG
    Jan 2 at 11:37



















  • ngModelChange is not working in my usecase

    – UdG
    Jan 2 at 11:37

















ngModelChange is not working in my usecase

– UdG
Jan 2 at 11:37





ngModelChange is not working in my usecase

– UdG
Jan 2 at 11:37


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005182%2fmat-slide-toggle-shouldnt-change-its-state-when-i-click-cancel-in-confirmation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$