未捕获的ReferenceError onKeyPress

时间:2018-03-18 19:58:57

标签: html angular typescript onkeypress

我想查看用户是否正在键入字母或数字。它在我使用@HostListener时有效,但在HTML中使用onKeyPress时则无效。

这是我的HTML

void DoublyLinkedList::SortedInsert(const int& new_element) {


    if (new_element != 0) {                      //creates new node np with value equal to new element
        Node* np = new Node(new_element); //if new element is not equal to zero and if its not head, it points to both                                              
                                                         head and  tail
        if (!Head)
        {
            Head = np;
            Tail = np;


        }
        else if (new_element < Head->Element)   //if new element less than element in Head np's  next pointer pointing to                                        
                                                        Head and Head prev pointer pointing to np and Head is pointing to np
        {
            np->Next = Head;
            Head->Prev = np;
            Head = np;


        }
        else                                     //if new element is greater than Head we take cur node which is pointing                                      
                                                   to Head's next pointer pointing to node after Head
        {

            Node *cur = Head->Next;
            while ((cur) && (new_element >= cur->Element))  //new element greater than Node after Head,cur will point to                                            cur's next pointer which is node after cur
                cur = cur->Next;

            if (cur)
            {                                            // ?? whats happening here
                np->Prev = cur->Prev;
                np->Next = cur;
                cur->Prev->Next = np;
                cur->Prev = np;
            }
            else                                         // ?? whats happening here
            {
                Tail->Next = np;
                np->Prev = Tail;
                Tail = np;
            }
        }


    }

这是函数detectLetter()

<div class="table">
 <div class="cell" *ngFor="let number of [_,1,2,3,4,5,6,7,8,9,10]">
  <input class="number" value="{{number}}" disabled>
 </div>
 <div class="row" *ngFor="let row of grid?.boxArray; let i = index">
  <input class="number" value="{{i+1}}" disabled>
   <div class="cell" *ngFor="let col of grid?.boxArray; let j = index">
    <input type="text" onKeyPress="detectLetter('$event')" id="{{i}}_{{j}}" class="box" maxLength="1"
           [ngClass]="{ 'sel': grid.boxArray[i][j].isSelectedHint, 'unsel': !grid.boxArray[i][j].isSelectedHint, 'black': grid.boxArray[i][j].isBlack }" >
   </div>
 </div>
</div>

1 个答案:

答案 0 :(得分:1)

你得到这个错误的原因是angular有自己的方法,比如(keypress),所以你不得不改变你的代码:

<div class="table">
 <div class="cell" *ngFor="let number of [_,1,2,3,4,5,6,7,8,9,10]">
  <input class="number" value="{{number}}" disabled>
 </div>
 <div class="row" *ngFor="let row of grid?.boxArray; let i = index">
  <input class="number" value="{{i+1}}" disabled>
   <div class="cell" *ngFor="let col of grid?.boxArray; let j = index">
    <input type="text" (keypress)="detectLetter($event)" id="{{i}}_{{j}}" class="box" maxLength="1"
           [ngClass]="{ 'sel': grid.boxArray[i][j].isSelectedHint, 'unsel': !grid.boxArray[i][j].isSelectedHint, 'black': grid.boxArray[i][j].isBlack }" >
   </div>
 </div>
</div>

我认为在你的情况下更好的方法是使用(keyup)