2017-12-28 14 views
1

ts(type script)ファイルからチェックボックスのチェック値を取得しようとしています。このために、私はブール変数を持っており、この変数値を使ってdivを表示して隠すことを目的としていますが、私は問題に直面しています。これを解決し、これを行うための正しい方法を私に教えてください。ここに私のコード...プロパティ 'checked'が 'HTMLElement'タイプの角4にありません

htmlコードは、私はその後、私が手に仕えるNG実行すると

**checkbox code**abcde" class="form-check-input" id="abcde" value="1" 
(change)="checked('abcde')"> abcde 

ショーと非表示のコード

*ngIf='shown' 

tsが

checked(value) { 

    let get_id = document.getElementById('abcde'); 

    if (get_id.checked == true) { 
     this.shown = true 
    } 
    else if (get_id.checked == false) 
     this.shown = false; 
} 

ファイルである「プロパティ'checked'はタイプ 'HTMLElement'に存在しません "

ありがとうございます!あなたのコンポーネントここで

import { Component, ViewChild, ElementRef } from '@angular/core'; 
@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    @ViewChild('abcde') abcde: ElementRef; 
     func(){ 
      this.abcde.nativeElement.checked 
     } 


} 
+0

あなたは反応型を使用していますか? –

+1

この 'const get_id = document.getElementById( 'abcde')をHTMLInputElementとして試してください; – yurzui

+3

[コードと再フォーマットされたテキスト](https://stackoverflow.com/editing-help#code)をあなたのサイトに挿入する方法を見てください。質問。あなたの投稿を編集してください。 – fjoe

答えて

2
//Typescript File (app.component.ts)   
    import { Component } from 'angular/core'; 
       @Component({ 
        selector: 'app-root', 
        template: './app.component.html', 
        styleUrls: ['./app.component.css'] 
       }) 
       export class AppComponent { 
        public shown = false; 
       } 

    //Html Code (app.component.html) 
     <form #myForm='ngForm'>  
       <input type="checkbox" class="form-control" 
        #checkBox="ngModel" 
        [(ngModel)]="shown" name="checkBox"> 
     </form> 
       <div *ngIf="shown"> 
        <!---Your Code Here...---> 
       </div> 

+0

あなたの答えをありがとう – raihan

1
あなたのHTMLで

<input #abcde type="checkbox" (change)="fun()" /> 

、これがショーを行うと、チェックボックスの選択と選択解除に基づいてのdiv要素を非表示にする方法の一つです。表示された変数を使用して双方向バインディングが行われます。

+0

あなたの答えに感謝しています..., – raihan

関連する問題