angularjs - Passing directives to Component in Angular 1.5 -
i'm using angular 1.5
library , wants create component simple text box below.
component js
'use strict'; angular .module('components') .component('inputbox', { bindings: { label: '@', name: '@', type: '@', classes: '@', placeholder: '@', maxlength: '@' }, controlleras: 'field', templateurl: 'app/components/inputbox.html' });
component template
<input type="{{field.type || 'text'}}" class="form-control {{field.classes}}" id="{{field.name}}" name="{{field.name || 'unnamed'}}" maxlength="{{field.maxlength}}" placeholder="{{field.placeholder}}" />
usage in templates.
<input-box label="enter account" name="accountnumber" type="number" classes="form-control" placeholder="account number" maxlength="20" // directives ng-model="accountnumber" ng-custom1="value1" ng-custom2="value2" ng-custom-validator="value4" />
i have 2 issues below need best practices.
- i want keep directives extended in usage template not part of component.
which best practice
@
or=
understanding options.a. "@" ( text binding / one-way binding )
b. "=" ( direct model binding / two-way binding )
c. "&" ( behaviour binding / method binding )
why approach?
i have around 27 forms many input types. want create single component have field label, input , error container.
there things quite confusing or wrong:
you passing name of model like
<input-box modelname="account.number"...
and try use with:
<input type="{{field.type || 'text'}}" ng-model="account.number" ...
you not using modelname, instead trying access object variable account.number
not defined (at least not defined in example) not dynamic anymore.
if want pass model directly like:
angular .module('components') .component('inputbox', { bindings: { model: '=', ... }, controlleras: 'field', templateurl: 'app/components/inputbox.html' });
with
<input-box model="account" ... />
and in component template:
<input ng-model="model" ... />
regarding second question: can´t
<input ... {{field.attribs}} />
you use attrs , copying them input element:
angular .module('components') .component('inputbox', { bindings: { model: '=', ... }, controlleras: 'field', templateurl: 'app/components/inputbox.html', controller: function($scope, $element, $attrs) { angular.foreach($attrs, function(key, value){ $element[key] = value; }); } });
at least wondering why wrap input element component , nothing more copy properties, want achieve?
Comments
Post a Comment