yuyang041060120 / angular2-ui-switch

angular2 ui switch component
http://yuyang041060120.github.io/angular2-ui-switch
MIT License
57 stars 36 forks source link

provoking click event twice #3

Closed onecib closed 7 years ago

onecib commented 8 years ago

Hi there, right now i am using ui-switch module in my angular 2 application. and would like to put a confirmation message (alert) while deactivating the ui-switch-boxes: ----> first case , the user confirms , and the ui-switch-box becomes active ----> second case, the user denies , and the ui-switch-case must stay inactive. is there any implementation for such an issue ? if no , is there any possibility to provoke the click event (or the change event ) twice ?

cheers Othman

yuyang041060120 commented 8 years ago

@xcuben try this:

<ui-switch [(ngModel)]="enable" (change)="onChange()"></ui-switch>
@Component({
    selector: 'app',
    template: require('./app.html')
})
export class AppComponent {
    enable: boolean = false;

    onChange() {
        if(!this.enable && !confirm('sure to change?')){
          setTimeout(() => {
            this.enable = false;
          });
        }
    }
}
onecib commented 8 years ago

@yuyang041060120 : I can't apply the ngModel on ui-switch tag . because for that i would need ngForm and i totally dont need it also. is there any other way to do it ?

yuyang041060120 commented 8 years ago

@xcuben so you just use onChange?

onecib commented 8 years ago

@yuyang041060120 I tried it , it is working in background (the value of enable is changing only if the user confirms) but the switch-button is switching without considering the user-response

yuyang041060120 commented 8 years ago

@xcuben I' m sorry you can't prevent the default switch behavior. But you can change it from checked to unchecked state when the user denies.