“角度2无法理解View#template”中的值使用$ event [英] "Angular 2 could not understand the value in View#template" using $event

查看:216
本文介绍了“角度2无法理解View#template”中的值使用$ event的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能得到$事件的工作。示例:

  import'package:angular2 / core.dart'; 

@Component(
selector:'my-app',
template:'''
< h1> {{title}}< / h1>
< button(click)=onTest($ event)>测试< / button>
''')
class AppComponent {
String title ='Testing event ;

onTest(event){
print(event);
}
}

当我让酒吧服务应用程序时,我会抛出一个构建错误:

 构建错误:
转换DirectiveProcessor on file_transfer | lib / app_component.dart抛出错误:角度2可能不了解View#模板中的值。
'''
< h1> {{title}}< / h1>
< button(click)=onTest($ event)>测试< / button ...
包:angular2 / src / transform / common / type_metadata_reader.dart 164:5 _expressionToString
包:angular2 / src / transform / common / type_metadata_reader.dart 920:17 _CompileTemplateMetadataVisitor._populateTemplate
包:angular2 / src / transform / common / type_metadata_reader.dart 901:9 _CompileTemplateMetadataVisitor.visitNamedExpression
包: analyzer / src / dart / ast / ast.dart 7455:15 NamedExpressionImpl.accept
...

我还尝试将(点击)更改为ng-click,如下所示 $在AngularDart 1.0中的事件不再可用了 - 可以选择什么?但是没有帮助。



我是新来的角色,但上面的代码应该工作,对吧?我查看了 https://webdev.dartlang.org/angular / guide / template-syntax#!#event-binding 和使用$ event的英雄示例。看不到任何差异...



任何帮助都不胜感激。

解决方案>

如果模板在自己的文件中,此代码段可以正常工作。



但是,由于模板位于 .dart 文件中,字符串中的任何 $变量将被评估为变量的值



在这种情况下,以下任一项将使其工作:


  1. 退出模板中的 $ 。替换

< button(click)=onTest($ event)> testing< / button> ;





<按钮)=onTest(\ $ event)>测试< / button>


  1. 使用原始字符串作为模板:

template:r'''
。 ..template内容...
''')


I can't get $event working. Example:

import 'package:angular2/core.dart';

@Component(
    selector: 'my-app', 
    template: '''
        <h1>{{title}}</h1>
        <button (click)="onTest($event)">testing</button>
    ''')
class AppComponent {
    String title = 'Testing event';

    onTest(event) {
        print(event);
    }
}

When I let pub serve the app, I throws a build error:

Build error:
Transform DirectiveProcessor on file_transfer|lib/app_component.dart threw error: Angular 2 could not understand the value in View#template.
'''
        <h1>{{title}}</h1>
        <button (click)="onTest($event)">testing</button...
package:angular2/src/transform/common/type_metadata_reader.dart 164:5   _expressionToString
package:angular2/src/transform/common/type_metadata_reader.dart 920:17  _CompileTemplateMetadataVisitor._populateTemplate
package:angular2/src/transform/common/type_metadata_reader.dart 901:9   _CompileTemplateMetadataVisitor.visitNamedExpression
package:analyzer/src/dart/ast/ast.dart 7455:15                          NamedExpressionImpl.accept
...

I also tried to change the (click) into ng-click as here $event in AngularDart 1.0 is not available anymore - what is the alternative? but it didn't help.

I'm new to angular, but the code above should work, right? I looked at https://webdev.dartlang.org/angular/guide/template-syntax#!#event-binding and the heroes example where $event is used too. Can't see any difference...

Any help is appreciated.

解决方案

This snippet would work fine if the template was in its own file.

However, since the template is in a .dart file, any $variable in a string will be evaluated to the value of variable in that context.

In this case, either of the following will make this work:

  1. Escape the $ in the template. Replace

<button (click)="onTest($event)">testing</button>

with

<button (click)="onTest(\$event)">testing</button>

  1. Use a raw string as the template:

template: r''' ...template content... ''')

这篇关于“角度2无法理解View#template”中的值使用$ event的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆