简体   繁体   English

为什么这个 function 每次都明确设置为更新 textarea 值一次?

[英]Why does this function updates textarea value only once when it is explicitly set to do this each time?

I am building a Vigenere encoding app to learn how to use Angular.我正在构建一个 Vigenere 编码应用程序来学习如何使用 Angular。 I have the following component and HTML.我有以下组件和 HTML。

I don't understand why the encode function works once and then when changing the textarea inputs, it doesn't want to encode anymore until I refresh the page again.我不明白为什么编码 function 工作一次,然后在更改 textarea 输入时,它不想再编码,直到我再次刷新页面。

Component:零件:

import { Component } from '@angular/core';
import { encode } from 'punycode';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'vigenere-cypher';
  plainText;
  key;
  encodedText;

  counter = 0;
  output = [];
  row = 0;
  column = 0;
  encode() {
    let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

    while(this.counter < this.plainText.length) {
      for(let i = 0; i < this.key.length; i++) {
        this.row = alphabet.indexOf(this.key[i]);
        this.column = alphabet.indexOf(this.plainText[this.counter]);
        this.output.push(alphabet[(this.row + this.column) % alphabet.length]);
        this.counter++;
        if (this.counter >= this.plainText.length) break;
      }
    }
    this.encodedText = this.output.join('');

  }
}

HTML: HTML:

<p class="lead">
            <a (click)="encode()" class="btn btn-primary btn-lg" href="#" role="button">Encode</a>
          </p>

      <div class="container">
        <div class="row">
          <div class="col-sm">
            <textarea [(ngModel)]="plainText" name=""  id="plainText" cols="30" rows="10"></textarea>
          </div>
          <div class="col-sm">
            <textarea [(ngModel)]="key" name=""  id="key" cols="30" rows="10"></textarea>
          </div>
          <div class="col-sm">
            <textarea [(ngModel)]="encodedText" name="" id="encodedText"  cols="30" rows="10"></textarea>
          </div>
        </div>
      </div>

What have I tried我试过什么

Putting a console.log statement inside the encode to see if the function executes each time.在encode里面放一个console.log语句,看function是否每次都执行。

You need to reset your counter inside your encode function.您需要在编码 function 中重置计数器。 Otherwise first it will retain its value after the first encode operation.否则首先它将在第一次编码操作后保留其值。

I think its better to have a local counter variable inside your encode method as you dont need it anywhere else.我认为最好在您的编码方法中有一个本地计数器变量,因为您在其他任何地方都不需要它。

The encode function is executed each time, but the while condition is not met because the count variable has stored the value of the first execution.每次执行encode function,但是因为count变量已经存储了第一次执行的值,所以不满足while条件。

Try reseting counter variable:尝试重置计数器变量:

encode() {
    this.counter = 0;
    let alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

    while (this.counter < this.plainText.length) {
      for (let i = 0; i < this.key.length; i++) {
        this.row = alphabet.indexOf(this.key[i]);
        this.column = alphabet.indexOf(this.plainText[this.counter]);
        this.output.push(alphabet[(this.row + this.column) % alphabet.length]);
        this.counter++;
        if (this.counter >= this.plainText.length) break;
      }
    }
    this.encodedText = this.output.join('');
}

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM