简体   繁体   English

如何更改 Angular 模板变量的值?

[英]How can I change the value of an Angular template variable?

I want to change the value of a template variable based on a condition.我想根据条件更改模板变量的值。

<ng-container *ngIf="'first' as myVar">
  <ng-container *ngIf="myVar == 'first'">
    <ng-container *ngIf="'later' as myVar">
      <div>{{myVar}}</div>
    </ng-container>
  </ng-container>

  <div>{{myVar}}</div>
</ng-container>

However I am getting但是我得到

later
first

instead of代替

later
later

(probably the second variable is different than the first one and exists only in the scope of the second variable (which seems to be a weird way to implement template variables)). (可能第二个变量与第一个变量不同,只存在于第二个变量的范围内(这似乎是实现模板变量的一种奇怪方式)。

Is there a simple way to change the value of a template variable?有没有一种简单的方法来更改模板变量的值?

I would also like to know if there is a way to join the condition ( "myVar == 'first'" ) and the assignment ( "'later' as myVar" ) into one expression instead of two nested <ng-container> .我还想知道是否有办法将条件( "myVar == 'first'" )和赋值( "'later' as myVar" )加入一个表达式而不是两个嵌套的<ng-container>

Template Variables模板变量

For your main question, your elements can only see the the template variables of their parents.对于您的主要问题,您的元素只能看到其父母的模板变量。

For example, this will fail to compile:例如,将无法编译:

<ng-container *ngIf="'later' as myVar">
  <div>{{myVar}}</div>
</ng-container>
<div>{{myVar}}</div> <!-- No parent TV or reference in component.ts -->

So your conclusion is right, in that it is a different value, although it isn't all that weird if you think of it as living on that DOM-Element.所以你的结论是正确的,因为它是一个不同的值,尽管如果你认为它是生活在那个 DOM-Element 上并不是那么奇怪。 They are more so meant as references to that particular element, and are indeed less useful for logic.它们更多地意味着对那个特定元素的引用,并且确实对逻辑不太有用。 For that you're better off binding to your component.ts file.为此,您最好绑定到您的component.ts文件。

In short, I don't think reassigning of Template Variables is possible.简而言之,我认为重新分配模板变量是不可能的。 You will just get the value of the closest parent.您将只获得最近父母的价值。

Second Question第二个问题

For your second question, this is a little shorter (but /w same result):对于你的第二个问题,这有点短(但 /w 相同的结果):

<ng-container *ngIf="'first' as myVar">
  <ng-container *ngIf="(myVar == 'first' ? 'later' : '') as myVar">
    <div>{{myVar}}</div>
  </ng-container>
  <div>{{myVar}}</div>
</ng-container>

You could also (ab)use ngTemplateOutlet to achieve something similar:您还可以(ab)使用ngTemplateOutlet来实现类似的功能:

<ng-template #s [ngTemplateOutlet]="s" let-a [ngTemplateOutletContext]="{ $implicit: 'first' }">
    {{a}}
    <ng-template #t [ngTemplateOutlet]="t" let-b [ngTemplateOutletContext]="{ $implicit: a == 'first' ? 'later' : '' }">
    {{b}}
    </ng-template>
</ng-template>

Here's a Stackblitz for you.这是给你的Stackblitz

Presentation Logic in Component.ts Component.ts 中的表示逻辑

It is actually perfectly fine to have logic for presentation in your component.在您的组件中包含用于表示的逻辑实际上是非常好的。 In fact it is pretty much the only thing that should be in your presentation components .事实上,它几乎是您的演示组件中应该包含的唯一内容。

It is a best practice to use the approach of Container & Presentation Components .最佳实践是使用Container & Presentation Components的方法。

Container component容器组件

  • Manages the state by interacting with service/store通过与服务/商店交互来管理状态
  • Contains only minor presentation, like for example a header/title仅包含次要演示文稿,例如标题/标题
  • Passes state to presentation components using input properties使用输入属性将状态传递给表示组件

Presentation component演示组件

  • Presents/renders the data, but does not directly interact with state呈现/渲染数据,但不直接与状态交互
  • Communicates with container component using output properties使用输出属性与容器组件通信

Angular allows you to set a ternary expression inside the structural directives such as *ngIf="argument === value ? true : false " Here is a stackblitz to show you this in action . Angular 允许您在结构指令中设置三元表达式,例如*ngIf="argument === value ? true : false " 这是一个堆栈闪电战向您展示这一点 (Change the value of name from Angular to React in the link to see how *ngIf="" will behave) (在链接中将 name 的值从 Angular 更改为 React 以查看*ngIf=""的行为方式)

Thus you do not need to nest *ngIf="" the way is done in your code above.You can also do the ternary expressions in the 'controller' - Typescript file and that's cleaner and more readable.因此,您不需要像上面的代码那样嵌套*ngIf="" 。您还可以在“控制器”-Typescript 文件中执行三元表达式,这样更清晰,更具可读性。

Such as:如:

public isAngular: boolean = this.name === 'Angular' ? true : false

And then you bind isAngular to your template *ngIf="" .然后将isAngular绑定到您的模板*ngIf=""

You can also use *ngIf="condition; else fallback" where fallback is a template reference to another content <ng-template #fallback>content goes here</ng-template>您还可以使用*ngIf="condition; else fallback"其中fallback是对另一个内容的模板引用<ng-template #fallback>content goes here</ng-template>

 <ng-container *ngIf="myVar === 'first'" else #later>
   <div>{{myVar}}</div>
   <div>{{myVar}}</div>
 </ng-container>

 <ng-template #later *ngIf="myVar === 'later'" >
   <div>{{myVar}}</div>
   <div>{{myVar}}</div>
 </ng-template>

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

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