Angular2 Click method always return false value
I have four input fields which need to validated at the backend. So i have written one service call and based on output i need to add class to the input field. So if OK then green else red.
I want to use same method for all as backend validation is same. So i am trying to return boolean value from the on click function but it always returning false. Here is my code:-
<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;
}
);
}
So want to know if its possible to return boolean from click function in angular2 and if yes, then how?
You should be returning the subscribed data as below
validateQuery(querystatus, query){
this._validatorService.validateQuery(query).subscribe(
(data) => {
return data;//////////////////////////
},
(err) =>{
return true;
}
);
}
Update 1
validateQuery(querystatus, query){
this._validatorService.validateQuery(query).subscribe(
(data) => {
this.queryok= false;
},
(err) =>{
this.queryok= true;
}
);
}
You function call in HTML will be as
(click)="validateQuery(query1ok,sampledetails.rules[0].query1)"
链接地址: http://www.djcxy.com/p/74984.html
上一篇: JavaScript中变量的意思是什么?