NativeScript / Angular - 使用操作栏

时间:2017-07-10 23:31:45

标签: nativescript angular-reactive-forms nativescript-angular

首先,我实施了以下代码。

app.component.html

<StackLayout [formGroup]="form">
    <label text="Name"></label>
    <TextField formControlName="Name"></TextField>   
    <label text="Last Name"></label>
    <TextField formControlName="LastName"></TextField>
    <button text="save" (tap)="save()"></button>
</StackLayout>

app.component.ts

public form: FormGroup;

constructor(private fb: FormBuilder){
    this.form = this.fb.group({
        "Name": ["", [Validators.required]],
        "LastName": ["", [Validators.required]]
    });
}

public save(){
    console.log(JSON.stringify(this.form.value));
}

当我运行上面的代码时,一切都还好。我正确地得到姓名和姓氏。

当我尝试向此代码添加操作栏时,会出现问题。

app.component.html

<ActionBar title="New" class="action-bar">        
    <ActionItem text="save" (tap)="save()"></ActionItem>       
</ActionBar>
<StackLayout [formGroup]="form">
   <label text="Name"></label>
   <TextField formControlName="Name"></TextField>   
   <label text="Last Name"></label>
   <TextField formControlName="LastName"></TextField>
   <button text="save" (tap)="save()"></button> 
</StackLayout>   

app.component.ts 保持不变。

当我运行此代码并点击stacklayout中的按钮时,我正确地获取了名称和姓氏,但是当我点击ActionItem时,我得到一个名称和姓氏的空字符串。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

你的代码看起来很好 - 实际上我已经用this test application重新测试了它,并且一切都按预期在我这边工作。

附注:如果在iOS上进行测试,请记住,如果后续控制台日志相同,则只能打印一次。 (this)因此,实际上操作完成时可能看起来没有打印日志。