2017-04-12 1 views
1

スクリプトでは、jquery(javascriptと同じ)を使用して要素の値を変更しますが、この要素に接続されたモデルは変更されませんでした。javascriptまたはjqueryで要素値を変更した後のangular2コンポーネントモデルの変更

<input type="text" class="form-control" id="username" placeholder="Username" [(ngModel)]="user.username"> 

私は手動でそのモデルが更新されるaswell値を変更します。ここでは、この要素です。それは何でしょうか?

おかげ

+0

あなたは、明確なアイデア –

+0

私は正確にあなたが実現したいものを、あなたは正確に私たちに知らせ – Maxim

+0

説明が必要なものを教えてくださいを得るより多くのではない手の込んだていただけます要件が明確ではありません –

答えて

-1

Seの例ここplunker

親コンポーネント:

import { Comp1Component } from './../comp1/comp1.component'; 
import { Component, OnInit } from '@angular/core'; 

@Component({ 
    selector: 'app-comp-parent', 
    template:` 
     <p>ngModel: {{sharedVarParent}}</p> 
     <app-comp1 [comp1]="sharedVarParent" (sharedVarChange)="onChange($event)"></app-comp1> 
     <hr /> 
     <app-comp2 [comp2]="sharedVarParent" (sharedVarChange)="onChange($event)"></app-comp2> 
    ` 
}) 
export class CompParentComponent implements OnInit { 
    sharedVarParent ='Initial'; 
    onChange(ev){ 
     this.sharedVarParent = ev; 
    } 
} 

COMP1:

import { Component, OnInit, Input, Output,EventEmitter } from '@angular/core'; 

@Component({ 
    selector: 'app-comp1', 
    template:` 
     <input type="text" id="username" placeholder="{{comp1}}" [ngModel]="comp1" (ngModelChange)="change($event)"> 
     <div>{{comp1}}</div>` 
}) 
export class Comp1Component implements OnInit { 
    @Input() comp1; 
    @Output() sharedVarChange = new EventEmitter(); 
    change(newValue) { 
    this.comp1 = newValue; 
    this.sharedVarChange.emit(newValue); 
    } 
} 

器Comp2

import { Component, OnInit, Input, Output,EventEmitter } from '@angular/core'; 

@Component({ 
    selector: 'app-comp2', 
    template:` 
    <input type="text" id="username" placeholder="{{comp2}}" [ngModel]="comp2" (ngModelChange)="change($event)"> 
    <div>{{comp2}}</div> 
    ` 
}) 
export class Comp2Component implements OnInit { 
    @Input() comp2; 
    @Output() sharedVarChange = new EventEmitter(); 
    change(newValue) { 
    this.comp2 = newValue; 
    this.sharedVarChange.emit(newValue); 
    } 
} 

enter image description here

+0

コンポーネントはDOMにネストする必要がありますか? @Input()と@Output()がどのように動作するのかわかりません – Maxim

+0

COmp1とCOmp2は兄弟であり、COmp-parentは両者の親であり、それらの間のリンクです。 Comp-parent(コンポーネントテンプレートとして)はapp.component.html内にあります – sTx

関連する問題