沈斌
2017-12-15 f9b157566af34b8dc28ba10b34d025ac04f3168b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Component } from '@angular/core';
import swal, { SweetAlertType } from 'sweetalert2';
 
@Component({
    selector: 'app-sweetalert',
    templateUrl: './sweetalert.component.html'
})
export class SweetAlertComponent {
    types = [ 'success', 'error', 'warning', 'info', 'question' ];
 
    alertType(type: SweetAlertType) {
        swal(`Try me! Example: ${type} modal`, '', type);
    }
 
    async alertText() {
        const { value: name } = await swal({
            title: 'What is your name?',
            input: 'text',
            inputPlaceholder: 'Enter your name or nickname',
            showCancelButton: true,
            inputValidator: function (value) {
                return !value && 'You need to write something!';
            }
        });
 
        if (name) {
            swal({type: 'success', title: 'Hi, ' + name});
        }
    }
 
    async alertHtml() {
        const {value: formValues} = await swal({
            title: 'Multiple inputs',
            html:
              '<input id="swal-input1" class="swal2-input">' +
              '<input id="swal-input2" class="swal2-input">',
            focusConfirm: false,
            preConfirm: function () {
              return [
                (document.querySelector('#swal-input1') as HTMLInputElement).value,
                (document.querySelector('#swal-input2') as HTMLInputElement).value
              ];
            }
        });
 
        if (formValues) {
            swal(JSON.stringify(formValues));
        }
    }
}