追加后如何等待按钮单击解析

时间:2019-05-17 12:21:20

标签: javascript angular angular7

我正在将html内容附加到服务的正文中。 (非常需要)

  1. 单击按钮如何调用该方法?
  2. 在用户单击按钮之前,我该如何解决问题

这是我的服务

import { Injectable, OnInit } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class AppConfig  {

    option:string;
    popup = `<div class="main-section">

        <div class="singin-options">
            <div class="form-check form-check-inline">
                <input class="form-check-input" (change)="onLoginChange($event)" value="CTS" type="radio" name="loginOption" id="inlineRadio1">
                <label class="form-check-label" for="inlineRadio1">Login as ABC Member</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" (change)="onLoginChange($event)" value="IBO" type="radio" name="loginOption" id="inlineRadio2">
                <label class="form-check-label" for="inlineRadio2">Login as XYZ Member</label>
            </div>
        </div>
        <div class="signin">
            <button type="button" class="btn btn-success">Sign In</button>
        </div>

    </div>`

    constructor(private store:Store<StateShared>){}

    initializeData() {

        return new Promise((resolve, reject) => {

            $('body').append(this.popup);//appended

            return setTimeout(() => {
                resolve(true) //get value from user, wait until
            }, 5000)

        });
    }

    onLoginChange(){ console.log('loo')}

}

1 个答案:

答案 0 :(得分:0)

我通过将所有事件添加到promise之内来解决了这个问题,

 return new Promise((resolve, reject) => {

            if(!localStorage.getItem("name")){

                $('body').append(this.popup);

                $("input[name=loginOption]:radio").change(function (event) {
                    that.userOption =  $(event.target).val();
                    $("button").prop('disabled', false);
                })

                $("button").prop('disabled', true);

                $("button").click(function () {
                    that.value = false;
                    localStorage.setItem("name", that.userOption);
                    resolve(true);

                })
            }

            if(localStorage.getItem("name")){
                resolve(true);
            }

        });

对我来说很好。