简体   繁体   English

TypeScript 中的变量链接是如何工作的?

[英]How does the variable linking work in TypeScript?

I am new at Angular and now I am currently being stucked, because I can not understand why it works like that.我是 Angular 的新手,现在我被困住了,因为我不明白为什么它会这样工作。 Here is my code ⬇️这是我的代码⬇️

🪧 Explonation of whole functionality: I have a mock list of heroes in another file (mock.heroes.ts), where the whole list is set like const value, what means you can not change it anymore. 🪧整体功能说明:我在另一个文件(mock.heroes.ts)中有一个模拟英雄列表,其中整个列表设置为 const 值,这意味着您不能再更改它。 Everything works well, also it's understandable for me, till you select, by clicking on exact hero, your selectedHero .一切正常,这对我来说也是可以理解的,直到您通过单击确切的英雄来选择selectedHero The div is now visible, and you can change its name in the input field below. div 现在可见,您可以在下面的输入字段中更改其名称。

❓ I can not understand, why when I change something inthe input with the id="hero-name", the change occurs not only on selectedHero.name but also on hero.name (in the ul with class="heroes"). ❓ 我不明白,为什么当我更改输入中的 id="hero-name" 时,更改不仅发生在selectedHero.name 上,而且发生在hero.name 上(在 class="heroes" 的 ul 中)。

❓ Is this.selectedHero like link to real hero , or how does this binding works in TS? ❓ this.selectedHero 是不是像真正的英雄的链接,或者这个绑定在 TS 中是如何工作的? At first I thought, selectedHero was a copy of a hero .起初我以为, selectedHero是一个英雄的副本。 That's why I am asking, because it does not make sence for me.这就是我问的原因,因为它对我没有意义。

❓ Why even after the list is set to be const value, you can change the names of the heroes init? ❓ 为什么list 设置为const 值后,还可以更改init 英雄的名字?

___.component.html ___.component.html

<ul class="heroes">
  <li 
    *ngFor="let hero of heroes" 
    (click)="onSelect(hero)" 
    [class.selected]="hero === selectedHero">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

<div *ngIf="selectedHero">
    <h2>{{selectedHero.name | uppercase}} Details</h2>

    <div><span>id: </span>{{selectedHero.id}}</div>

    <div>
      <label for="hero-name">Hero name: </label>
      <input id="hero-name" [(ngModel)]="selectedHero.name" placeholder="name">
    </div>
</div>

___.component.ts ___.component.ts

import { Hero } from '../hero';
import { HEROES } from '../mock-heroes';
import { Component, OnInit } from '@angular/core';

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

  heroes = HEROES;
  selectedHero?: Hero;

  constructor() { }

  ngOnInit(): void {
  }

  onSelect(hero: Hero) {
    this.selectedHero = hero;
  }
}

mock.heroes.ts mock.heroes.ts

import { Hero } from './hero';

export const HEROES: Hero[] = [
  { id: 11, name: 'Dr Nice' },
  { id: 12, name: 'Narco' },
  { id: 13, name: 'Bombasto' },
  { id: 14, name: 'Celeritas' },
  { id: 15, name: 'Magneta' },
  { id: 16, name: 'RubberMan' },
  { id: 17, name: 'Dynama' },
  { id: 18, name: 'Dr IQ' },
  { id: 19, name: 'Magma' },
  { id: 20, name: 'Tornado' }
];
  • I can not understand, why when I change something inthe input with the id="hero-name", the change occurs not only on selectedHero.name but also on hero.name (in the ul with class="heroes")我无法理解,为什么当我使用 id="hero-name" 更改输入中的某些内容时,更改不仅发生在 selectedHero.name 上,而且发生在 hero.name 上(在 class="heroes" 的 ul 中)

Angular runs checks every now and then to look for updates on your models. Angular 不时运行检查以查找模型的更新。 If a change is detected, the view is updated, this is why your content changes elsewhere.如果检测到更改,则视图会更新,这就是您的内容在其他地方更改的原因。

  • Is this.selectedHero like link to real hero, or how does this binding works in TS? this.selectedHero 像真正英雄的链接,或者这个绑定在 TS 中是如何工作的? At first I thought, selectedHero was a copy of a hero.一开始我以为,selectedHero是英雄的副本。 That's why I am asking, because it does not make sence for me.这就是我问的原因,因为它对我没有意义。

It's the same object, they share the same address.这是同一个对象,它们共享同一个地址。 If you want to deepcopy your object, call JSON.parse(JSON.stringify(myJSONobject))如果要JSON.parse(JSON.stringify(myJSONobject))对象,请调用JSON.parse(JSON.stringify(myJSONobject))

  • Why even after the list is set to be const value, you can change the names of the heroes init为什么即使list设置为const值后,你也可以更改英雄init的名字

What const keyword does is protect your variable from changing it's address. const关键字的作用是保护您的变量免于更改其地址。 Let's say you have an array at address A having multiple objects in it.假设您在地址 A 处有一个数组,其中包含多个对象。 Each an every object has it's own address (B, C, D, ...).每个对象都有自己的地址(B、C、D、...)。 So as long as you don't reasign that const variable you are good to go.因此,只要您不重新指定 const 变量,您就可以开始使用。 nothing hold you from changing the inner content of this array.没有什么可以阻止您更改此数组的内部内容。 Reassign it is what wouldn't work using the const keyword.使用const关键字无法重新分配它。

It is the so called two way binding underneath.这就是所谓的双向绑定 You change that object by entering a name.您可以通过输入名称来更改该对象。 Because that object has changed, that triggers the rerendering of that one object in the ul and you got the magic happened 😉.因为那个对象发生了变化,这会触发ul中那个对象的重新渲染,然后神奇的事情发生了😉。 Read here: https://angular.io/guide/two-way-binding在这里阅读: https : //angular.io/guide/two-way-binding

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

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