简体   繁体   English

Angular2无法使用JavaScript导入FormsModule

[英]Angular2 unable to import FormsModule with javascript

I am doing the Angular2 Heroes Tutorial: https://angular.io/docs/ts/latest/tutorial/toh-pt1.html in plain Javascript (not Typescript) and i am having trouble to Import the Forms Module as mentioned: (description is only for Typescript): 我正在用纯Javascript(不是Typescript)编写Angular2 Heroes教程: https ://angular.io/docs/ts/latest/tutorial/toh-pt1.html,并且我无法按上述方式导入表单模块:(说明仅适用于Typescript):

Before we can use two-way data binding for form inputs, we need to import the FormsModule package in our Angular module. 在将双向数据绑定用于表单输入之前,我们需要在Angular模块中导入FormsModule包。 We add it to the NgModule decorator's imports array. 我们将其添加到NgModule装饰器的imports数组中。 This array contains the list of external modules used by our application. 该数组包含我们的应用程序使用的外部模块的列表。

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { AppComponent }  from './app.component';
@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

When i am adding the Forms module to my app.module.js import array, it fails to find the module: 当我将Forms模块添加到app.module.js导入数组时,它找不到该模块:

zone.js:129 Uncaught Error: Unexpected value 'undefined' imported by the module 'class2' zone.js:129未捕获的错误:模块“ class2”导入了意外的值“ undefined”

Here is my app.module.js: 这是我的app.module.js:

(function(app) {
  app.AppModule =
    ng.core.NgModule({
      imports: [ ng.platformBrowser.BrowserModule, ng.common.FORM_DIRECTIVES],
      declarations: [ app.AppComponent],
      bootstrap: [ app.AppComponent ]
    })
    .Class({
      constructor: function() {}
    });
})(window.app || (window.app = {}));

in my node_modules folder the Forms module exists. 在我的node_modules文件夹中,存在Forms模块。 If i remove "ng.common.FORM_DIRECTIVES" from the imports array, no error is thrown. 如果我从导入数组中删除“ ng.common.FORM_DIRECTIVES”,则不会引发任何错误。

The Content of console.dir(ng) is: console.dir(ng)的内容为:

Object
    common:Object
    compiler:Object
    core:Object
    coreTokens:Object
    platformBrowser:Object
    platformBrowserDynamic:Object
    probe
    :
    inspectNativeElement(element /** TODO #9100 */)
    __proto__:Object

The content of console.dir(ng.forms) is: console.dir(ng.forms)的内容为:

undefined

I am sorry, i found the error. 对不起,我发现了错误。 As so often it was nothing having to do with typescript or angular, i just had to add the script tag in the index.html file to load the forms.umd.js: 通常,它与Typescript或angular没有任何关系,我只需要在index.html文件中添加script标签以加载forms.umd.js:

<script src="../node_modules/@angular/forms/bundles/forms.umd.js"></script>

now i can import the Forms Module with the following code and i can use the ngModule functionality: 现在,我可以使用以下代码导入表单模块,并且可以使用ngModule功能:

ng.core.NgModule({
  imports: [ ng.platformBrowser.BrowserModule, ng.forms.FormsModule],
  declarations: [ app.AppComponent],
  bootstrap: [ app.AppComponent ]
})

Try using import { FORM_DIRECTIVES } from 'angular2/common'; 尝试import { FORM_DIRECTIVES } from 'angular2/common';使用import { FORM_DIRECTIVES } from 'angular2/common';

Source: Angular 2 two way binding using ngModel is not working 来源: Angular 2使用ngModel的两种方式绑定不起作用

EDIT: 编辑:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FORM_DIRECTIVES } from 'angular2/common';
import { AppComponent }  from './app.component';
@NgModule({
  imports: [
    BrowserModule,
    FORM_DIRECTIVES
  ],
  declarations: [
    AppComponent
  ],
  bootstrap: [ AppComponent ]
})
export class AppModule { }

If this doesn't work, post the console.dir(ng); 如果这不起作用,请发布console.dir(ng);。 again( with this code in use ). 再次(使用此代码)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM