简体   繁体   English

使用Angular2以编程方式创建Html局部变量

[英]Create Html local variable programmatically with Angular2

I need to know if there is a way to create HTML local variables programmatically. 我需要知道是否有一种以编程方式创建HTML本地变量的方法。

I am developing a web app where I have an NgFor loop and I want to be able to assign a local variable to each sub element created by the NgFor. 我正在开发一个Web应用程序,我有一个NgFor循环,我希望能够为NgFor创建的每个子元素分配一个局部变量。

ie : 即:

<div *ngFor="#elt of eltList" >
    <span #setLocalVariable(elt.title)></span>
</div>

setLocalVariable(_title : string){
    let var = do some stuff to _title;
    return var;
}

The exemple above shows you what I am trying to accomplish and obviously does not work. 上面的例子显示了我想要完成的事情,显然不起作用。 Is there a way to achieve this ? 有没有办法实现这个目标?

Thank you in advance. 先感谢您。

Edit: 编辑:

After seeing the answers I got (and i thank everyone who took the time to read my question and tried to answer it) i'll explain a bit more why i want it that way. 看到我得到的答案后(我感谢所有花时间阅读我的问题并尝试回答的人)我会再解释一下为什么我会这样想。

I will be using : loadIntoLocation() from the DynamicComponentLoader . 我将使用: DynamicComponentLoader loadIntoLocation() That function got as a 3rd parameter a string that refers to an anchors (ie : #test in an html element). 该函数作为第3个参数得到一个引用锚点的字符串(即:html元素中的#test )。 Thats why i need to create those local variables with a name equal to the one of my elt.title . 这就是为什么我需要创建名称等于我的elt.title名称的局部变量。

I think local variables (defined with the # character) don't apply for your use case. 我认为局部变量(用#字符定义)不适用于您的用例。

In fact, when you define a local variable on an HTML element it corresponds to the component if any. 实际上,当您在HTML元素上定义局部变量时,它对应于组件(如果有)。 When there is no component on the element, the variable refers to the element itself. 当元素上没有组件时,变量引用元素本身。

Specifying a value for a local variable allows you to select a specific directive associated with the current element. 指定局部变量的值允许您选择与当前元素关联的特定指令。 For example: 例如:

<input #name="ngForm" ngControl="name" [(ngModel)]="company.name"/>

will set the instance of the ngForm directive associated with the current in the name variable. 将设置与name变量中当前关联的ngForm指令的实例。

So local variables don't target what you want, ie setting a value created for the current element of a loop. 因此局部变量不会以您想要的为目标,即设置为循环的当前元素创建的值。

If you try to do something like that: 如果你尝试做类似的事情:

<div *ngFor="#elt of eltList" >
  <span #localVariable="elt.title"></span>
  {{localVariable}}
</div>

You will have this following error: 您将遇到以下错误:

Error: Template parse errors:
There is no directive with "exportAs" set to "elt.title" ("
  <div *ngFor="#elt of eltList" >
    <span [ERROR ->]#localVariable="elt.title"></span>
    {{localVariable}}
  </div>
"): AppComponent@2:10

Angular2 actually looks for a directive matching the provided name elt.title here)... See this plunkr to reproduce the error: https://plnkr.co/edit/qcMGr9FS7yQD8LbX18uY?p=preview Angular2实际上在这里查找与提供的名称elt.title相匹配的指令... ...请参阅此plunkr以重现错误: https ://plnkr.co/edit/qcMGr9FS7yQD8LbX18uY?p = preview

See this link: http://victorsavkin.com/post/119943127151/angular-2-template-syntax , section "Local variables" for more details. 有关详细信息,请参阅此链接: http//victorsavkin.com/post/119943127151/angular-2-template-syntax ,“局部变量”部分。

In addition to the current element of the iteration, ngForm only provides a set of exported values that can be aliased to local variables: index , last , even and odd . 除了迭代的当前元素之外, ngForm仅提供一组导出值,这些值可以别名为局部变量: indexlastevenodd

See this link: https://angular.io/docs/ts/latest/api/common/NgFor-directive.html 请看以下链接: https//angular.io/docs/ts/latest/api/common/NgFor-directive.html


What you could do is to create a sub component to display elements in the loop. 你可以做的是创建一个子组件来显示循环中的元素。 It will accept the current element as parameter and create your "local variable" as attribute of the component. 它将接受当前元素作为参数,并创建“局部变量”作为组件的属性。 You will be able then to use this attribute in the template of the component so it will be created once per element in the loop. 然后,您就可以在组件的模板中使用此属性,以便在循环中为每个元素创建一次。 Here is a sample: 这是一个示例:

@Component({
  selector: 'elt',
  template: `
    <div>{{attr}}</div>
  `
})
export class ElementComponent {
  @Input() element;

  constructor() {
    // Your old "localVariable"
    this.attr = createAttribute(element.title);
  }

  createAttribute(_title:string) {
    // Do some processing
    return somethingFromTitle;
  }
}

and the way to use it: 以及使用它的方式:

<div *ngFor="#elt of eltList" >
  <elt [element]="elt"></elt>
</div>

Edit 编辑

After your comment, I think that you try the approach described in this answer. 在你的评论之后,我认为你尝试了这个答案中描述的方法。 Here are more details: create dynamic anchorName/Components with ComponentResolver and ngFor in Angular2 . 以下是更多详细信息: 在Angular2中使用ComponentResolver和ngFor创建动态anchorName / Components

Hope it helps you, Thierry 希望它对你有帮助,蒂埃里

You could stick it into the template interpolation since it handles expressions. 您可以将其粘贴到模板插值中,因为它处理表达式。

<div *ngFor="#elt of eltList" >
    <span>{{setLocalVariable(#elt)}}</span>
</div>

setLocalVariable(_title : string){
    let var = do some stuff to _title;
    return var;
}

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

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