无法获取隐藏的价值来自新窗口(角/ JavaScript的)

问题描述 投票:0回答:1

我有分量的形式构建

HTML

 <button type="button" (click)="myForm(i)">

打字稿

  myForm(i) {
   let form = document.createElement('form');
   form.setAttribute('action', this.urlAddr);
   form.setAttribute('id', 'test');
   form.setAttribute('target', this.name);
   let method = this.name === 'query' ? 'GET' : 'POST';
   form.setAttribute('method', method);
   let hiddenField = document.createElement('input');
    hiddenField.setAttribute('type', 'hidden');
    hiddenField.setAttribute('name', 'type');
    hiddenField.setAttribute('value', '1');
    form.appendChild(hiddenField);
    let hiddenFieldTwo = document.createElement('input');
    hiddenFieldTwo.setAttribute('type', 'hidden');
    hiddenFieldTwo.setAttribute('name', 'sas');
    hiddenFieldTwo.setAttribute('value', 'cnt');
    form.appendChild(hiddenFieldTwo);
   let token = document.createElement('input');
   token.setAttribute('id', 'token');
   token.setAttribute('type', 'hidden');
   token.setAttribute('name', 'secureToken');
   token.setAttribute('value', this.securityToken);
   form.appendChild(token);
   document.cookie = 'Flag=Y';
   document.body.appendChild(form);
   form.submit();
}

现在2号窗口打字稿我试着检索字段“令牌”,但它一直在说的价值是不确定的。

打字稿新窗口

  ngOnInit() {
    console.log('test'); //this prints but non below prints in new window
    let inputElement: HTMLInputElement = document.getElementById('token') as HTMLInputElement;
    let inputTwoElement: HTMLInputElement = document.getElementsByName('secureToken')[0] as HTMLInputElement;
    if (inputElement) { 
      const token: string = inputElement.value;
      console.log('target token =' + token);
    }
    if (inputTwoElement) {
      const token: string = inputTwoElement.value;
      console.log('target tokenTwo =' + token);
    }
}

我为什么我不能得到任何价值困惑。该请求是“GET”请求并将该值应该能够从父窗口加载。我在做什么错误或丢失的东西吗?

javascript angular typescript
1个回答
0
投票

如果所有的你的代码是在同一模块中,尝试使用ViewChild和ElementRef获得访问元素下面的例子:

import { Component, ElementRef, ViewChild, AfterViewInit} from '@angular/core';

@Component({
    selector: 'app',
    template: `
        <div #myivId>Some text</div>
    `, }) export class AppComponent implements AfterViewInit {
    @ViewChild('myDivId') myDiv: ElementRef;

    ngAfterViewInit() {
        console.log(this.myDiv.nativeElement.innerHTML);
    } }

然而,如果两个你张贴的所有代码都在不同的元素,他们没有获得对方的DOM。在构建之前他们每个人都有自己的文件,这一切都将在一个页面的HTML你建立之后。

侧面说明,如果你的目标是分享组件之间数据的最形式上正确的方法是使用一个服务,这就是的Angulars核心优势之一。

不过,也有可以达到你想要什么,我会推荐这读取各种方式:https://angularfirebase.com/lessons/sharing-data-between-angular-components-four-methods/

© www.soinside.com 2019 - 2024. All rights reserved.