使用 nativescript 2/angular 2,数据绑定有问题。我熟悉 angularJS 1.x,但我读过的文档应该可以工作。尝试了 ngModel 的不同变体,但不起作用。record.name 的值为“未定义”。
记录类只定义了一个 id 和 name 字段。我的另一个问题是如何触发组件的更改事件?如果用户开始输入文本输入,我如何在他们输入时调用组件中的函数?
下面是我的“html”:
<StackLayout>
<Textfield hint="Search for a Record" [(ngModel)]="record.name" (returnPress)="searchRecord()"></Textfield>
<Label text="{{ record.name }}"></Label>
<Button text="Search" class="btn" (tap)="searchRecord()"></Button>
<Button text="Take Photo" class="btn" (tap)="takePhoto()"></Button>
</StackLayout>
添加记录组件:
import {Component} from "@angular/core";
import cameraModule = require("camera");
import imageModule = require("ui/image");
import {Record} from "../../shared/record/record";
import {RecordService} from "../../shared/record/record-service";
@Component({
selector: "add-record",
templateUrl: "pages/add-record/add-record.html",
styleUrls: ["pages/add-record/add-record-common.css"],
providers: [ RecordService ]
})
export class AddRecordPage {
record: Record;
constructor(private _recordService: RecordService) {
this.record = new Record();
}
searchRecord() {
console.log(this.record.name + '!');
this._recordService.add(this.record)
.subscribe(
() => {
alert('a');
},
() => {
alert('b');
}
);
}
takePhoto() {
cameraModule.takePicture().then(picture => {
console.log("Result is an image source instance");
var image = new imageModule.Image();
image.imageSource = picture;
});
}
}
谢谢!