Html 如何通过 id 获取值元素 - Angular 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44636855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:44:49  来源:igfitidea点击:

How get value element by id - Angular 4

htmlangularrest

提问by Lau13

I'm working on REST API, I have one app beck-end in Java 1.6 and another front-end in Angular 4. I want to get the value by id of the HTML tag, I tried use DOM like this: <HTMLInputElement>document.getElementById(this.keyList[i].keyId)).value;but the problem is I can't use this sintax "this.keyList[i].keyId" because in class Key I have only one constructer, that fetch the attributes in JSON, so, when I go do key.keyId, said it does not exist. Does it exist another away to solve this problem, with propertys of ng? Thanks people.

我正在研究 REST API,我在 Java 1.6 中有一个应用程序后端,在 Angular 4 中有另一个前端。我想通过 HTML 标记的 id 获取值,我尝试使用 DOM 像这样:<HTMLInputElement>document.getElementById(this.keyList[i].keyId)).value;但是问题是我不能使用这个语法“this.keyList[i].keyId”,因为在 Key 类中我只有一个构造函数,它获取 JSON 中的属性,所以,当我去做 key.keyId 时,说它不存在. 是否存在另一个解决此问题的方法,具有 ng 的属性?谢谢人们。

HTML Code:

HTML代码:

<!-- Breadcrumbs -->
<div class="row">
<div class="col-md-12">
    <ol class="breadcrumb">
        <li routerLinkActive="active"><a routerLink="/">Início</a></li>
        <li>Consulta</li>
    </ol>
</div>
</div>
<div class="row">
<div class="col-md-12">
    <fieldset>
        <legend>Consultar Dados Telemáticos</legend>
        <form>
            <!-- PROVIDERS -->
            <div class="row">
                <div class="col-md-4">
                    <label>Provedor</label> <span class="required">*</span> 
<br>
                    <select class="form-control 
(ngModel)]="selectedProvider"
                        (change)="getKey(selectedProvider)" name="provider">
                        <option *ngFor="let providers of providerHeaderList"
                            [ngValue]="providers">{{providers.description}}
</option>
                    </select>
                </div>
            </div>
            <!-- KEYS DYNAMIC -->
            <br />
            <div class="row">
                <div class="col-md-4" *ngFor="let keys of keyList">
                    <label>{{keys.description}}</label>
                    <span class="required">*</span>
                    <input [id]="keys.keyId" class="form-control" 
type="text" />
                </div>
            </div>
            <!-- DATE PICK -->
            <br />
            <div class="row" *ngIf="!keyList?.length==0">
                <div class="col-md-4">
                    <label>Data de Início da Pesquisa</label>
                    <input class="form-control" type="date" 
placeholder="dd/mm/aaaa" data-original-title title />
                </div>
            </div>
            <!-- BUTTON SEARCH -->
            <br />
            <br />
            <div class="row" *ngIf="!keyList?.length==0">
                <div class="col-md-4">
                    <button class="btn btn-primary btn-sm ng-binding" 
(click)="btnSearchClick()">Pesquisar</button>
                </div>
                <div class="col-md-12">
                    <span><small>* Campos de preenchimento obrigatório.
</small></span>
                </div>
            </div>
        </form>
    </fieldset>
</div>
</div>
<!-- RESULTS TABLE -->      
<app-provider-result></app-provider-result>

TypeScript Code:

打字稿代码:

// Angular
import { Component, OnInit, ElementRef } from '@angular/core';
// Servi?os
import { ProviderService } from '../../services/provider.service';
import { KeyService } from '../../services/key.service';

import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
// Observable class extensions
import 'rxjs/add/observable/of';
// Observable operators
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
// Modelos
import { Provider } from '../provider';
import { Key } from '../../key/key';

@Component( {
selector: 'app-provider-search',
templateUrl: './provider-search.component.html',
styleUrls: ['./provider-search.component.css']
} )
export class ProviderSearchComponent implements OnInit {

providerHeaderList: Provider[] = [];
keyList: Key[] = [];
selectedProvider = null;
keyValues: string[] = [];

constructor( private providerService: ProviderService, private keyService: KeyService, private myElement: ElementRef ) { }

ngOnInit() {
    this.getProviders();
}

getProviders() {
    this.providerService.getAllHeaders().subscribe( p => {
        this.providerHeaderList = p;
    } )
}

getKey(selectedProvider){
    this.keyService.getAllKeyByProviderId(selectedProvider.id, selectedProvider.description).subscribe( k => {
        this.keyList = k;
    });
}

btnSearchClick(){
}

}

回答by Rahul Singh

You can make use of template refernce

您可以使用模板引用

Like

喜欢

<input type = "text" value = "demo" #ref>
<button (click) = "submit(ref.value)">submit</button> 

Now in ts you can use like

现在在 ts 你可以使用像

ref.value // template val
submit(value){ console.log("Reference value" + value) }