在Angular2中正确使用Elvis运算符以获取Dart组件的视图 [英] Correct use of Elvis operator in Angular2 for Dart component's view
问题描述
我在Angular2 Dart中看到了 Angular for Dart速查表(v2.0.0-beta .2)支持Elvis运算符.
I see in Angular2 Dart Angular for Dart Cheat Sheet (v2.0.0-beta.2) that the Elvis operator is supported.
我正在尝试在组件视图中使用它,但似乎这里不允许这样做.
I am trying to use it in an a components view but it seems that it is not allowed here.
给予
<div
<label
for = "first">First<span class = "require">*</span></label>
<input
type = "text"
[(ngModel)] = "name?.first"
id = "first">
.dart
...
Name name = new Name();
...
运行该应用程序会出现以下错误:
Running the application gives the following error:
Transform TemplateCompiler on
epimss_ng2_reg|lib/components/name_component.ng_meta.json threw error: Template parse errors:
Parser Error: The '?.' operator cannot be used in the assignment at column 13 in [name?.first=$event] in NameComponent@39:12 ("
<input
type = "text"
[ERROR ->][(ngModel)] = "name?.first"
#firstCtrl = "ngForm"
[ngFormControl] = "nameForm"): NameComponent@39:12
在什么情况下可以在组件视图中使用运算符?
What circumstance can I use the operator in a component's view?
欢呼
推荐答案
如错误消息所述,它不能用于分配. name
为null时,应将值分配到哪里?
如果将简短形式拆分为更明确的形式
As the error message says it can't be used for assignment. Where should the value be assigned to when name
is null?
If you split the short form to the more explicit one
[ngModel]="name?.first" (ngModelChange)="name.first=$event"
然后可以使用elvis运算符.
then you can use the elvis operator.
这篇关于在Angular2中正确使用Elvis运算符以获取Dart组件的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!