Two Way Data Binding Angular 2 in Electron not working

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:11:42

问题


I used electron-forge to create a new electron project that uses angular 2

npm install -g electron-forge electron-forge init -t angular2

Then I added @angular/forms

yarn add @angular/forms

Then I edited the app.components.ts file to look like this

import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'App',
  template:
  `<div>
    <h2>Welcome to {{name}} Angular2!</h2>
    {{name}}
    <input [(ngModel)] = "name">
  </div>`
})
export class AppComponent implements OnInit {
  public name = 'electron-forge';

  ngOnInit(): void {
    console.log('component initialized');
  }
}

@NgModule({
  imports: [BrowserModule, FormsModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

Two way binding is not working...when I change the text in my input control, the name filed is not changing

What am I missing?


回答1:


I figured out the problem.

I had to load zone.js and reflect-metadata in index.html instead of bootstrap.ts. So when I did this

  <script src="../node_modules/zone.js/dist/zone.js"></script>
  <script src="../node_modules/reflect-metadata/Reflect.js"></script>
  <script src="bootstrap.ts"></script>

This worked. I had to remove this

import 'zone.js';
import 'reflect-metadata';

From bootstrap.ts




回答2:


Since it might be because you forgot to add FormsModule in the module.ts file.Check your app.module.ts

import { FormsModule } from '@angular/forms';
...
imports: [ ..., FormsModule, ...]

Related question



来源:https://stackoverflow.com/questions/41906986/two-way-data-binding-angular-2-in-electron-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!