角三元表达式运行多行代码 [英] Angular ternary expression that runs more than one line of code
本文介绍了角三元表达式运行多行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个示例代码,其中一种形式的
可以正常工作。
so I have this example of code, in one of my forms, it works perfectly.
ng-submit="
commentCtrl.edit(comment.id, comment.text);
comment.edit=false;
"
另一方面,当我尝试在三元ng-keyup中运行多个命令时,出现了问题,角度无法解析。
on the other hand, when I try to run multiple "commands" in a ternary ng-keyup, things go wrong, angular can't parse it.
ng-keyup="
($event.keyCode == 13 && !$event.shiftKey)
?
commentCtrl.edit(comment.id, comment.text);comment.edit=false
:
return"
也尝试过:
($event.keyCode == 13 && !$event.shiftKey)
?
commentCtrl.edit(comment.id, comment.text) && comment.edit=false
:
return"
请帮助我!
推荐答案
在标记中编写这样的表达式是一种不好的做法。
It's a bad practice to write such expressions inside of markup.
无论如何,您都可以通过将值放入数组来做到这一点:
Anyway you can do it by putting the values into an array:
ng-submit="[commentCtrl.edit(comment.id, comment.text), comment.edit=false]"
ng-keyup="($event.keyCode == 13 && !$event.shiftKey)
? [commentCtrl.edit(comment.id, comment.text), comment.edit=false]
: 0"
这篇关于角三元表达式运行多行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文