I am currently working on creating a set of dynamic components that can be imported into a main component. It is much more convenient to nest components inside one another when I can pass all objects into a single binding instead of creating multiple bindings for each object being passed. To demonstrate what I am trying to achieve, I have set up a plunkr. My goal is to pass an object from a parent component into a child component's ng-model without the need for a separate binding.
I am wondering if this is possible and if anyone can provide suggestions or explanations as to why the nested component only updates the model locally and not in the entire view?
In essence, I would like the cNestedComponent to function in the same manner as the cDirectComponent, where the databinding updates from both inside and outside of the component's template.
http://plnkr.co/edit/vusx9rm1DnkbBlNBGyZG?p=preview
MARKUP:
<h1> Data Comment => {{ data.comment }} </h1>
<c-direct-input plplaceholder="direct component" plmodel="data.comment" pltype="text"></c-direct-input>
<c-nested-input input-bindings="{type: 'text', model: 'data.comment', placeholder: 'nested component'}"></c-nested-input>
COMPONENTS:
app.component('cNestedInput', {
template: '\
<h2> Nested Component </h2>\
<p style="display: block;"> {{ $ctrl.inputBindings.model }} </p>\
<input type="{{ $ctrl.inputBindings.type }}" placeholder="{{$ctrl.inputBindings.placeholder}}" ng-model="$ctrl.inputBindings.model" />\
',
bindings: {
inputBindings: '='
},
controller: function($scope) {}
});
app.component('cDirectInput', {
template: '\
<h2> Direct Component </h2>\
<p style="display: block;"> {{ $ctrl.plmodel }} </p>\
<input type="{{ $ctrl.pltype }}" placeholder="{{ $ctrl.plplaceholder }}" ng-model="$ctrl.plmodel" />\
',
bindings: {
plplaceholder: '@',
plmodel: '=',
pltype: '@'
},
controller: function($scope) {}
});
========================================================
UPDATE
Following feedback from user Julien Tassin, I have updated the plunker to be cleaner and to better demonstrate what I am aiming for:
https://plnkr.co/edit/cvYAdB?p=preview
The Direct Component examples show a clear path to achieving my goal, but I prefer not having to list out every single binding as components get nested. For instance:
<c-nested-input input-bindings="$ctrl.input.inputBindings"/>
is much simpler to type out than having to write out this
<c-direct-input input-placeholder="{{$ctrl.inputPlaceholder}}" input-type="{{$ctrl.inputType}}" input-model="$ctrl.inputModel"/>\
each time I want to nest the input component within a parent component.
This update should provide further clarification on what I am looking to achieve.