我需要创建一个多项选择题,其中正确答案显示在四个按钮之一上(我不知道哪一个,因为这将随机生成)。然后用户必须通过按下按钮选择正确的答案。
我的问题是:我如何编写“If”语句,当按下按钮时,当我不知道哪个按钮是正确的时?
这是代码
答案 0 :(得分:1)
试试这个:
import { Component, Input, OnInit } from '@angular/core';
import { MyData} from './mydata';
import { ActivatedRoute } from '@angular/router';
import { OtherService } from './other.service';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import { Headers, RequestOptions } from '@angular/http';
import { ParameterService } from '../parameter.service';
import { LocalStorageService } from 'angular-2-local-storage';
@Component({
templateUrl: 'app/test/test.component.html'
})
export class TestComponent implements OnInit {
subscription: any;
customerId: string;
// set getter as a property on the variable `year`:
////year: string;
get year(): string {
console.log("test.component.get()");
const value = this._parameterService.p['year'] + "";
// Any other code you need to execute when the data is changed here
this.getDataFromApi();
return value;
}
private sub: any;
errorMessage: string;
data: MyData[];
constructor(private _otherService: OtherService,
private _parameterService: ParameterService,
private _route: ActivatedRoute,
private _localStorageService: LocalStorageService) { }
getDataFromApi() {
// store route parameters in Local Storage
// if none found, try reading from Local Storage
if (this.customerId == null)
this.customerId = this._localStorageService.get('customerId') + "";
//if (this.year== null)
// this.year = this._localStorageService.get('year') + "";
this._localStorageService.set('customerId', this.customerId);
//this._localStorageService.set('year', this.year);
// try getting YEAR (not customerId, yet) from getter
this._otherService.getDataFromApi(this.customerId, this.year)
.then(
data => this.data = data,
error => this.errorMessage = <any>error);
}
getData(): void{
this.data = this._otherService.getData();
}
resetComponentState(): void {
this.getDataFromApi();
}
ngOnInit(): void {
this.sub = this._route.params.subscribe(params => {
this.customerId = params['customerId'];
//this.year = params['year'];
this.resetComponentState(); // based on new parameter this time
});
}
}