简体   繁体   English

Angular 2奇怪的模板作用域行为

[英]Angular 2 weird template scope behavior

Just went through the Tour of Heroes tutorial app and experienced some interesting behavior within my template. 刚经历了《英雄旅》教程应用程序,并在我的模板中经历了一些有趣的行为。 I started the second part of the tutorial with the following code: 我使用以下代码开始了本教程的第二部分:

class Hero {
  id: number;
  name: string;
}

@Component({
  selector: 'my-app',
  template:`
    <h1>{{title}}</h1>
    <h2>{{hero.name}} details!</h2>
    <div><label>id: </label>{{hero.id}}</div>
    <div>
      <label>name: </label>
      <div><input [(ng-model)]="hero.name" placeholder="name"></div>
    </div>
    `,
  directives: [FORM_DIRECTIVES]
})

class AppComponent {
  public title = 'Tour of Heroes';
  public hero: Hero = {
    id: 1,
    name: 'Windstorm'
  };
}

When instructed to add the array of new heroes ( var HEROES: Hero[] = [ /* Hero Data */]; ) and the new property to my component, I assumed we were replacing the original hero property and ended up with this: 当指示添加新英雄数组( var HEROES: Hero[] = [ /* Hero Data */]; )和新属性到我的组件时,我假设我们正在替换原始的hero属性,并最终得到以下结果:

class AppComponent {
  public title = 'Tour of Heroes';
  public heroes = HEROES;
}

Next, the template was modified like so: 接下来,对模板进行如下修改:

<h1>{{title}}</h1>
<ul class="heroes">
  <li *ng-for="#hero of heroes">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>
<h2>{{hero.name}} details!</h2>
<div><label>id: </label>{{hero.id}}</div>
<div>
  <label>name: </label>
  <div><input [(ng-model)]="hero.name" placeholder="name"></div>
</div>

In the browser, the unordered list then rendered one li per hero in the array, but did not print the name or id . 然后,在浏览器中,无序列表为数组中的每个hero渲染了一个li ,但未打印nameid Crazy. 疯。 After some tinkering, I realized that if I added the original hero property back to the AppComponent class all of the heroes in the array rendered just fine. 经过一番修补后,我意识到,如果将原始的hero属性添加回AppComponent类,则数组中的所有AppComponent都可以正常显示。 Also, if I simply removed any template code referencing the hero property not in the ng-for loop the list would also render just fine. 另外,如果我只是删除了所有ng-for循环中引用hero属性的模板代码ng-for则列表也可以正常显示。

Here is what I expected: 这是我所期望的:

  1. In my original version, all of the heroes in the array should be reflected in the unordered list, but then all of the hero values outside of the loop should be undefined or possibly the last item in the list. 在我的原始版本中,数组中的所有英雄都应反映在无序列表中,但是循环外的所有英雄值都应未定义,或者可能是列表中的最后一项。

  2. When I added back the original hero property, there should be some sort of name collision or some other side effect. 当我添加回原始的hero属性时,应该有某种名称冲突或其他副作用。

How does this work the way it does? 这是如何工作的?

Edit: Here is the requested plunker: http://plnkr.co/edit/U3bSCaIOOjFdtw9XxvdR?p=preview 编辑:这是请求的插件: http ://plnkr.co/edit/U3bSCaIOOjFdtw9XxvdR?p=preview

Ok so with your plunker I got a bit more of what you were trying to do and the issues you were having. 好吧,有了您的朋克小子,我可以了解到您正在尝试做的事情以及遇到的问题。

My Working plunk with more details is HERE 我的工作细节在这里

1. NG-For 1. NG-For

When you do a NG-For loop the "#" variable is isolated to be inside the loop only. 进行NG-For循环时,“#”变量被隔离为仅在循环内。 I renamed it "domhero" so you can see. 我将其重命名为“ domhero”,以便您可以看到。

You had a few calls to it OUTSIDE of the li object which wouldn't work. 您在li对象之外对它进行了几次调用,这是行不通的。 I re-wrote it a bit to put all your titles and stuff inside the LI loop. 我重新编写了一下,将所有标题和内容放入LI循环中。

2. Variables from the object 2.来自对象的变量

On the input you were trying to access a variable from inside the ng-for loop which you cant do. 在输入中,您试图从ng-for循环中访问您无法执行的变量。 Once you close out of a loop you cant access those variables. 一旦退出循环,就无法访​​问这些变量。 So I showed where I was binding to, to make it clearer for you. 所以我展示了我要绑定的位置,以便为您弄清楚。

I think it got confusing when you had so many things named the same thing all over the place (hero, heroes, HEROES, class: hero) if you take a look at the plunker I made I renamed the variables to help mark where they are coming from. 我想当您到处有这么多东西命名相同的东西(英雄,英雄,英雄,职业:英雄)时,会感到困惑,如果您查看一下插栓程序,我将其重命名为变量,以帮助标记它们的位置来自(哪里。

Hope it helps! 希望能帮助到你!

p

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

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