2017-08-15 12 views
1

1.5に変更され、コンポーネントの通信をよりよく理解しようとしています。別のコンポーネントのテンプレートの中にコンポーネントを入れることができますか?もしそうなら、彼らはどうやって互いにコミュニケーションを取るのですか?Angular 1.5を使用する - 別のコンポーネントのテンプレート内にコンポーネントを置くことはできますか?

+0

この質問は広範囲に及ぶ。 –

答えて

1

はい。各コンポーネントには入力と出力があります。入力は親から子へ、出力は子から親へと行われます。このplunker exampleでは、実際の値の増分は親コンポーネントのコントローラで行われますが、値は子コンポーネントに表示されます(インクリメントボタンがある)。

.component('parentComponent', { 
    template: '<child-component value="$ctrl.value" on-increment="$ctrl.increment()"></child-component>', 
    controller: function() { 
     // Init 
     var self = this; 
     self.$onInit = function() { 
     // 
     } 
     self.value = 7; 

     // Increment 
     self.increment = function() { 
     return self.value += 1; 
     } 
    }, 
    bindings: { 
     // 
    } 
}) 
.component('childComponent', { 
    template: '<h2 ng-bind="$ctrl.value"></h2><button ng-click="$ctrl.onIncrement()">Increment Value</button>', 
    controller: function() {}, 
    bindings: { 
     value: '<', 
     onIncrement: '&' 
    } 
}); 
関連する問題