在Angular 6中,可以使用@ViewChild指令来获取输入元素的值,然后在*ngIf中判断是否隐藏输入元素。
首先,在组件的HTML模板中添加输入元素和一个按钮,用来触发隐藏输入元素的操作。同时,在输入元素上添加一个标记,以便在组件中通过@ViewChild获取到它的引用并访问其值。
然后,在组件的代码中使用@ViewChild来获取输入元素的引用,并在隐藏输入元素之前获取输入元素的值。
import { Component, ViewChild } from '@angular/core';
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent {
@ViewChild('myInput') myInput: ElementRef;
isInputHidden = false;
hideInput() {
// 获取输入元素的值
const inputValue = this.myInput.nativeElement.value;
// 执行其他操作...
// 隐藏输入元素
this.isInputHidden = true;
}
}
在上面的代码中,@ViewChild('myInput') myInput: ElementRef;用来获取输入元素的引用,然后在hideInput()方法中,可以通过this.myInput.nativeElement来访问输入元素,并获取其值。
最后,通过设置isInputHidden变量为true,来隐藏输入元素。
这样,在输入元素隐藏之前就可以获取输入元素的值了。