【发布时间】:2017-06-25 09:42:44
【问题描述】:
我正在使用用户的输入填充一个数组,并希望在同一页面上呈现该数组中的数据。我正在使用 *ngFor 来做到这一点,但它会在彼此之上显示元素。 html 是:
<Gridlayout class="main-container" columns="auto" rows="auto,auto,auto">
<StackLayout exampleTitle toggleNavButton row="0" class="set-hour">
<!-- >> creating-timepicker-html -->
<timePicker class="example-container" #timePicker (loaded)="configure(timePicker)" verticalAlignment="center"></timePicker>
<Button text="Add hour" (tap)="onTap()"></Button>
<!-- << creating-timepicker-html -->
</StackLayout>
<StackLayout row="1" class="hour" *ngFor="let hour of hours">
<Label [text]='hour.hour'></Label>
<Label [text]='hour.minute'></Label>
</StackLayout>
</Gridlayout>
ts 是:
import { Component, OnInit, ViewChild, ElementRef} from "@angular/core";
import { UserService} from "../../../services/user.service";
import { TimePicker } from "ui/time-picker";
import { HoursModel } from "../../../models/hours.model";
@Component({
selector: "hours",
templateUrl:"components/profile/hours/hours.component.html",
styleUrls:["components/profile/hours/hours.component.css"]
})
export class HoursComponent implements OnInit{
@ViewChild("timePicker") tp: ElementRef;
public hours:Array<HoursModel>=[];
public index:number=0;
constructor(){}
ngOnInit(){}
configure(timePicker: TimePicker) {
timePicker.hour = 21;
timePicker.minute = 0;
}
onTap() {
let time =this.tp.nativeElement;
this.hours.push(<HoursModel>{id:this.index,hour:time.hour,minute:time.minute});
console.log(JSON.stringify(this.hours));
this.index++;
}
}
【问题讨论】:
标签: css arrays angular nativescript ngfor