我有一个使用Angular 6的模板驱动的表单。表单的组件嵌套在父组件中。在表单中,我有两个输入字段,一旦组件启动,我需要在其中输入错误(errors.required)。所以我有一个事件发射器。
我一直在寻找与ngInit类似的东西(来自AngularJs),所以我可以调用将事件发射到父组件的函数,但找不到。因此,我需要在组件中执行此操作。
因此,为了获取组件中的输入字段,我使用了ViewChild。
所以我的组件看起来像:
#pragma once
#include <fstream>
#include <string>
#include <vector>
namespace igg {
namespace io_tools {
/// Dummy structure to store relevant image data.
struct ImageData {
int rows;
int cols;
int max_val;
std::vector<int> data;
};
/// Reads from a pgm image from ascii file. Returns empty ImageData if the path
/// is not found or any errors have occured while reading.
ImageData ReadFromPgm(const std::string& file_name);
/// Write image data into an ascii pgm file. Return true if successful.
bool WriteToPgm(const ImageData& image_data, const std::string& file_name);
} // namespace io_tools
} // namespace igg
然后,当我运行应用程序时,出现此错误:
类型'ElementRef <任何>
中不存在属性“错误”我尝试使用@Component({
selector: 'my-form',
template: ... `<input ... #myInput="ngModel">` ....
})
export class myForm implements OnInit, AfterViewInit {
@ViewChild('myInput') private myInput: ElementRef;
...
...
ngAfterViewInit() {
console.log(this.myInput.errors);
和@ViewChild('myInput', {read: ElementRef})
仍然收到此错误消息。
现在,如果我仅控制台myInput,则不会收到错误消息,并且在控制台中,我可以看到错误键。我注意到的一件事,它显示在控制台中: ngModel {... ...错误:{..
所以我也尝试记录myInput.ngModel.errors-仍然出现错误
有什么主意吗?
谢谢。