从ngAfterViewInit()内的组件提交角度为2的表单

时间:2017-09-28 12:18:33

标签: html forms angular typescript

我尝试使用onInit()加载并初始化组件后,从组件中提交角度为2的表单;

以下是我的相同代码

app.component.html

<form #myForm class="hidden" name="myForm" action="{{formLink}}" method="post">
  <input class="hidden" type="text" id="token" name="token" value="{{token}}" />
  <input class="btn btn-primary pull-right hidden" #submitmyForm id="submitmyForm" type="submit" value="Submit"
  />
</form>

app.component.ts

@ViewChild('myForm') myForm: NgForm;
  @ViewChild('submitmyForm') submitmyForm: ElementRef;
  ngOnInit() {
    this.articleLink = localStorage.getItem('articleLink'); 
  }
  ngAfterViewInit() {
    const event = new MouseEvent('click', { bubbles: true });
    this.renderer.invokeElementMethod(
      this.submitmyForm.nativeElement, 'dispatchEvent', [event]);// Dosent Work
    this.submitmyForm.nativeElement.click();//Dosent Work   
}

0 个答案:

没有答案