Sobes.tech
Middle

Kā var īstenot divvirzienu sasaisti input elementam?

sobes.tech AI

Atbilde no AI

  1. Individuāls:

    Izmantojot JavaScript, sasaistīt input vērtību ar mainīgo un atjaunināt input, kad mainīgais mainās. Sekot input notikumam elementā un atjaunināt mainīgo:

    let myValue = '';
    
    const inputElement = document.getElementById('myInput');
    const displayElement = document.getElementById('display');
    
    inputElement.addEventListener('input', (event) => {
      myValue = event.target.value;
      displayElement.textContent = myValue; // Attēla atjaunināšana
    });
    
    // Piemērs, kā atjaunināt mainīgo no citas vietas, kas jāatjaunina input
    function updateValue(newValue) {
      myValue = newValue;
      inputElement.value = myValue; // Input atjaunināšana
      displayElement.textContent = myValue; // Attēla atjaunināšana
    }
    
  2. Izmantojot ietvarus/bibliotēkas:

    Tādas ietvaru kā Angular, Vue.js, React (ar kontrolētiem komponentiem) nodrošina iebūvētas mehānismus divvirzienu datu sasaistīšanai:

    • Angular: [(ngModel)]="myValue"
    • Vue.js: v-model="myValue"
    • React: Izmantojot value un onChange stāvokļa pārvaldībai:
    import React, { useState } from 'react';
    
    function MyComponent() {
      const [myValue, setMyValue] = useState('');
    
      const handleChange = (event) => {
        setMyValue(event.target.value);
      };
    
      return (
        <div>
          <input type="text" value={myValue} onChange={handleChange} />
          <p>Vērtība: {myValue}</p>
        </div>
      );
    }
    
  3. Proxy API izmantošana:

    Izveidot Proxy objektu, kas reaģē uz īpašības izmaiņām un atjaunina input vērtību, kā arī seko input notikumam un atjaunina Proxy īpašību:

    const inputElement = document.getElementById('myInput');
    const data = { value: '' };
    
    const reactiveData = new Proxy(data, {
      set(target, property, newValue) {
        if (property === 'value') {
          target[property] = newValue;
          inputElement.value = newValue; // Input atjaunināšana
          document.getElementById('display').textContent = newValue; // Attēla atjaunināšana
          return true;
        }
        return false;
      }
    });
    
    inputElement.addEventListener('input', (event) => {
      reactiveData.value = event.target.value; // Proxy īpašības atjaunināšana
    });
    
    // Piemērs, kā atjaunināt Proxy īpašību no citas vietas
    // reactiveData.value = 'Jauna vērtība';
    
  4. Custom Elements ar Shadow DOM un šabloniem:

    Izveidot lietotāja definētu elementu, kas inkapsulē input un īsteno sasaistes loģiku savā Shadow DOM:

    class TwoWayInput extends HTMLElement {
      constructor() {
        super();
        const shadowRoot = this.attachShadow({ mode: 'open' });
        shadowRoot.innerHTML = `
          <input type="text" />
          <p id="display"></p>
        `;
    
        this.inputElement = shadowRoot.querySelector('input');
        this.displayElement = shadowRoot.getElementById('display');
    
        this._value = '';
    
        this.inputElement.addEventListener('input', (event) => {
          this.value = event.target.value;
        });
      }
    
      get value() {
        return this._value;
      }
    
      set value(newValue) {
        this._value = newValue;
        this.inputElement.value = newValue;
        this.displayElement.textContent = newValue;
        this.dispatchEvent(new CustomEvent('change', { detail: this._value }));
      }
    
      connectedCallback() {
        // Iespējams, inicializācijas vērtība no atribūta
        if (this.hasAttribute('initial-value')) {
          this.value = this.getAttribute('initial-value');
        }
      }
    }
    
    customElements.define('two-way-input', TwoWayInput);