我是angular2的新手,也不是专业的。我有一个父子组件,当用户单击一个按钮时,子组件的选择器会显示一些东西。所以它应该是条件,并且从子组件中使用它。下面是我的代码:父Html:
<div *ngIf="page2">
<add-property-page2></add-property-page2>
</div>
<div class="text-center col-sm-12">
<button [disabled]="!propertyForm.form.valid"
(click)="onSubmit($event,value,amlak)"
type="submit" class="btn btn-success">Continue
</button>
</div>和父组件:
import {Component, OnInit, ViewChild, ViewEncapsulation, Input } from '@angular/core';
import {Amlak} from '../models/amlak';
import {AddPropertyPage2Component} from './add-paroperty-page2.component';
@Component({
selector: 'add-property',
styleUrls: ['./selection.css'],
templateUrl: './add-property.html',
providers: [DataService, Amlak],
})
export class AddPropertyComponent {
private page2: boolean;
@ViewChild(AddPropertyPage2Component)
private addPropertyPage2: AddPropertyPage2Component;
constructor(private amlak: Amlak, private dataService: DataService
) {
this.userLogined = AuthenticationService.check();
}
onSubmit($event:any,value,amlak) {
$event.preventDefault();
if (this.page2) {
this.amlak.emkanat = this.addPropertyPage2.emkan.filter(e => e.checked ==
true);
}
}现在我的问题是,当用户单击时,在父对象中显示子对象的选择器的最简单方法是什么。因为错误是:无法读取未定义的属性'emkan‘。我知道这是因为*ngIf,但我不知道我该怎么办。另外,我应该说我可以像这样在代码中使用viewchild方法。非常感谢您的帮助。
发布于 2018-03-12 21:36:01
你的问题有点老了,但我也有同样的问题,也许我可以用我的答案帮助其他人。
*ngIf将HTML元素从DOM中移除,因此ViewChild在DOM中找不到该元素。
首先,我想使用ngShow指令。但是在Angular 5中它已经不存在了。但在版本5中,您可以很容易地绑定到隐藏属性,这不会从DOM中删除元素,而是隐藏它,就像ngShow指令一样,ViewChild可以再次找到该元素。
https://stackoverflow.com/questions/46168891
复制相似问题