Angular2 Click方法始终返回false值

时间:2017-05-05 02:44:59

标签: javascript jquery angular angular2-forms angular2-services

我有四个输入字段需要在后端验证。所以我写了一个服务调用,基于输出我需要在输入字段中添加类。所以如果OK,那么绿色也是红色。

我想对所有人使用相同的方法,因为后端验证是相同的。所以我试图从点击功能返回布尔值,但它总是返回false。这是我的代码: -

  <div class="row"><div class="col-md-2 col-md-offset-2 nopadding"><div class="form-group   pull-right"><label>Query1:</label></div></div><div class="col-md-4 col-md-offset-0"><div class="form-group input-group"><input type="text" [ngClass]="{red: !(query1ok),green: (query1ok)}" class="form-control" name="query1" id="query1" [(ngModel)]="sampledetails.rules[0].query1"><span class="input-group-btn"><a  href="javascript:void(0)" class="btn btn-default" alt="validate query" (click)="[query1ok]:validateQuery(query1ok,sampledetails.rules[0].query1)" type="button"><i class="fa fa-play"></i></a></span></div></div></div> <div class="row"><div class="col-md-2 col-md-offset-2 nopadding"><div class="form-group   pull-right"><label>Query2:</label></div></div><div class="col-md-4 col-md-offset-0"><div class="form-group input-group"><input type="text" [ngClass]="{red: !(query2ok),green: (query2ok)}" class="form-control" name="query2" id="query2" [(ngModel)]="sampledetails.rules[0].query2"><span class="input-group-btn"><a  href="javascript:void(0)" class="btn btn-default" alt="validate query" (click)="validateQuery(query2ok,samplerdetails.rules[0].query2)" type="button"><i class="fa fa-play"></i></a></span></div></div></div><div class="row"><div class="col-md-2 col-md-offset-2 nopadding"><div class="form-group   pull-right"><label>Query3:</label></div></div><div class="col-md-4 col-md-offset-0"><div class="form-group input-group"><input type="text" [ngClass]="{red: !(query3ok),green: (query3ok)}" class="form-control" name="query3" id="query3" [(ngModel)]="samplerdetails.rules[0].query3"><span class="input-group-btn"><a  href="javascript:void(0)" class="btn btn-default" alt="validate query" (click)="validateQuery(query3ok,ssampledetails.rules[0].query3)" type="button"><i class="fa fa-play"></i></a></span></div></div></div><div class="row"><div class="col-md-2 col-md-offset-2 nopadding"><div class="form-group   pull-right"><label>Query4:</label></div></div><div class="col-md-4 col-md-offset-0"><div class="form-group input-group"><input type="text" [ngClass]="{red: !(query4ok),green: (query4ok)}" class="form-control" name="query4" id="query4" [(ngModel)]="samplerdetails.rules[0].query4"><span class="input-group-btn"><a  href="javascript:void(0)" class="btn btn-default" alt="validate query" (click)="validateQuery(query4ok,sampledetails.rules[0].query4)" type="button"><i class="fa fa-play"></i></a></span></div></div></div>


validateQuery(querystatus, query){
    this._validatorService.validateQuery(query).subscribe(
            (data) => {
                 return false;
            },
            (err) =>{
            return true;
            }
        );
    }

所以想知道它是否可以从angular2中的click函数返回布尔值,如果是,那么如何?

1 个答案:

答案 0 :(得分:1)

您应该按以下方式返回订阅数据

validateQuery(querystatus, query){
    this._validatorService.validateQuery(query).subscribe(
            (data) => {
                 return data;//////////////////////////
            },
            (err) =>{
            return true;
            }
        );
    }

更新1

validateQuery(querystatus, query){
    this._validatorService.validateQuery(query).subscribe(
            (data) => {
                 this.queryok= false;
            },
            (err) =>{
                 this.queryok= true;
            }
        );
    }

您在HTML中的函数调用将为

(click)="validateQuery(query1ok,sampledetails.rules[0].query1)"