简体   繁体   English

无法绑定到'ngModel',因为它不是已知的本机属性或已知指令

[英]Can't bind to 'ngModel' since it isn't a known native property or known directive

I am trying to learn angular2dart and follow the tutorial from anguar2dart site . 我正在尝试学习angular2dart并遵循anguar2dart网站的教程。

Consider following code: 考虑以下代码:

import 'package:angular2/core.dart';

class Hero {
  final int id;
  String name;
  Hero(this.id, this.name);
}

@Component(
    selector: 'my-app',
    template: '''
      <h1>{{title}}</h1>
      <h2>{{hero.name}} details!</h2>
      <div><label>id: </label>{{hero.id}}</div>
      <div>
        <label>name: </label>
        <input [(ngModel)]="hero.name" placeholder="name">
      </div>'''
)
class AppComponent {
  String title = 'Tour of Heroes';
  Hero hero = new Hero(1, 'Windstorm');
}

When I compiled this, it shows me the error message: 当我编译它时,它显示错误消息:

Build error:
Transform TemplateCompiler on Sample|lib/app_component.ng_meta.json threw error: Template parse errors:
Can't bind to 'ngModel' since it isn't a known native property or known directive. Please fix typo or add to directives list. ("
      <div>
        <label>name: </label>
        <input [ERROR ->][(ngModel)]="hero.name" placeholder="name">
      </div>"): AppComponent@5:15

What am I doing wrong? 我究竟做错了什么?

It seems you are missing transformers for common directives in your pubspec.yaml file. 您的pubspec.yaml文件中似乎缺少常见指令的变换器。

Check https://github.com/angular-examples/toh-1/blob/master/pubspec.yaml it contains following transformers angular section: 检查https://github.com/angular-examples/toh-1/blob/master/pubspec.yaml它包含以下变换器角度部分:

transformers:
- angular2:
    platform_directives:
    - 'package:angular2/common.dart#COMMON_DIRECTIVES'
    platform_pipes:
    - 'package:angular2/common.dart#COMMON_PIPES'
    entry_points: web/main.dart

As of Angular 4, platform_directives is gone. 从Angular 4开始, platform_directives就不见了。

NgModel is a form directive now. NgModel现在是一个表单指令。 You can include form directives as follows: 您可以包含表单指令,如下所示:

import 'package:angular_forms/angular_forms.dart' as forms;

@Component(
    selector: 'my-app',
    directives: const[forms.formDirectives],
)
class AppComponent {
}

Using dart 2.3.0 And following the tutorials. 使用dart 2.3.0并按照教程进行操作。 It's necessary to add directives: [formDirectives], as part of your component. 有必要添加directives: [formDirectives],作为组件的一部分。 And import import 'package:angular_forms/angular_forms.dart'; 并导入import 'package:angular_forms/angular_forms.dart';

import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';

import 'hero.dart';

@Component(
  selector: 'my-app',
  template: '''
    <h1>{{title}}</h1>
    <h2>{{hero.name}}</h2>
    <div><label>id: </label>{{hero.id}}</div>
    <div>
      <label>name: </label>
      <input [(ngModel)]="hero.name" placeholder="name">
    </div>
  ''',
  directives: [formDirectives],
)
class AppComponent {
  final title = 'Tour of Heroes';
  Hero hero = Hero(1, 'Windstorm');
}

Check the repo with the tutorial example, fortunately, has been updated as long as Dart releases updates: https://github.com/angular-examples/toh-1/blob/master/lib/app_component.dart 请使用教程示例检查repo,幸运的是,只要Dart发布更新,就会更新: https//github.com/angular-examples/toh-1/blob/master/lib/app_component.dart

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

相关问题 无法绑定到“ ngModel”,因为它不是“ select”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'select' 无法绑定到“ ngModel”,因为它不是“ textarea”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'textarea' “无法绑定到‘ngModel’,因为它不是‘输入’的已知属性” - “Can't bind to 'ngModel' since it isn't a known property of 'input' ” 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’ 无法绑定到“ngModel”,因为它不是“输入”的已知属性 - Can't bind to 'ngModel' since it isn't a known property of 'input' 错误:无法绑定到“ ngModel”,因为它不是的已知属性 - Error: Can't bind to 'ngModel' since it isn't a known property of 无法绑定到“ ngFor”,因为它不是已知的本机属性 - Can't bind to 'ngFor' since it isn't a known native property “无法绑定到&#39;ngFor&#39;,因为它不是已知的本机属性” - “Can't bind to 'ngFor' since it isn't a known native property” 无法绑定到&#39;href&#39;,因为它不是已知的本机属性 - Can't bind to 'href' since it isn't a known native property 无法绑定到&#39;ngFormControl&#39;,因为它不是已知的本机属性 - Can't bind to 'ngFormControl' since it isn't a known native property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM