如何在 ngIf 中赋值
How to assign values in ngIf
我是 Angular 的新手。我正在学习指令。我正在使用 primeng
中的 pTooltip
。但是我想根据条件在pTooltip中显示文本。
让我们以 Stack Overflow 本身为例。您不能对一个答案投票两次。如果用户没有对该特定文章投赞成票,则他应该得到 msg1,否则他应该得到 msg2。其中:
msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
这是我的代码:
article.component.html
<button
class="btn btn-success"
*ngIf="allowUpvote ? pTooltip={{msg1}} : pTooltip={{msg2}}
[disabled]="!allowUpvote">
UPVOTE
</button>
article.component.ts
...
allowUpvote=true;
msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
...
ngOnInit() {
for(var i=0;i<5;i++) {
if(this.currentLoggedInUserId==this.articleDetails.upvotersList[i]) {
this.allowUpvote=false; //It will be set false if the user is there in the upvoters list
}
}
});
但是我收到这个错误:
Parser Error: Bindings cannot contain assignments at column 24 in [allowUpvote ? pTooltip={{msg1}} : pTooltip={{msg2}}]
请给我一些指示。
应该是
[pTooltip]="(allowUpvote) ? msg1 : msg2"
编辑:这意味着您应该放弃 *ngIf,在这种情况下您不需要它。
试试这个
[pTooltip] = "allowUpvote ? msg1 : msg2"
使用[],解释器知道读取和执行“”之间的代码
已更新,更好地解释为 Plochie
When you say pTooltip="value" that means use value provided in "" as parameter for pTooltip. Here you have not binded the value from component, you have just given the constant. To have databinding you need []. Now after [], it means that bind whatever written in "" with the pTooltip and hence it will execute the code inside "" and the output for that statement then will be treated as input for pTooltip
我是 Angular 的新手。我正在学习指令。我正在使用 primeng
中的 pTooltip
。但是我想根据条件在pTooltip中显示文本。
让我们以 Stack Overflow 本身为例。您不能对一个答案投票两次。如果用户没有对该特定文章投赞成票,则他应该得到 msg1,否则他应该得到 msg2。其中:
msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
这是我的代码:
article.component.html
<button
class="btn btn-success"
*ngIf="allowUpvote ? pTooltip={{msg1}} : pTooltip={{msg2}}
[disabled]="!allowUpvote">
UPVOTE
</button>
article.component.ts
...
allowUpvote=true;
msg1="I found this article very useffeul.";
msg2="You've already upvoted this article";
...
ngOnInit() {
for(var i=0;i<5;i++) {
if(this.currentLoggedInUserId==this.articleDetails.upvotersList[i]) {
this.allowUpvote=false; //It will be set false if the user is there in the upvoters list
}
}
});
但是我收到这个错误:
Parser Error: Bindings cannot contain assignments at column 24 in [allowUpvote ? pTooltip={{msg1}} : pTooltip={{msg2}}]
请给我一些指示。
应该是
[pTooltip]="(allowUpvote) ? msg1 : msg2"
编辑:这意味着您应该放弃 *ngIf,在这种情况下您不需要它。
试试这个
[pTooltip] = "allowUpvote ? msg1 : msg2"
使用[],解释器知道读取和执行“”之间的代码
已更新,更好地解释为 Plochie
When you say pTooltip="value" that means use value provided in "" as parameter for pTooltip. Here you have not binded the value from component, you have just given the constant. To have databinding you need []. Now after [], it means that bind whatever written in "" with the pTooltip and hence it will execute the code inside "" and the output for that statement then will be treated as input for pTooltip