My directive and angular 1.5 component
.directive('externalUpdate', ['$parse', function($parse) { return { link: function(scope, element, attrs){ var setterFunc = $parse(attrs.ngModel).assign; var func = scope.$eval(attrs.externalUpdate); func(element, function(value) { scope.$apply(function() { setterFunc(scope, value); }); }); } }; }]) .component('datebox', { bindings: { size: '@@?', name: '@@', text: '@@', model: '=', classes: '@@?', disabled: '=?', time: '<?' }, controller: function() { if (!this.size) { this.col1 = '6'; this.col2 = '6'; } else { var size = parseInt(this.size); this.col1 = size.toString(); this.col2 = (12 - size).toString(); } this.updateInput = function(element, setter) { element.on("dp.change", function() { setter(element.val()); }); } }, template: String.raw` <div class="form-group"> <label ng-if="$ctrl.col1!='0'" for="{{::$ctrl.name}}" class="col-md-{{::$ctrl.col1}} control-label">{{::$ctrl.text}}</label> <div class="col-md-{{::$ctrl.col2}}"> <input type="text" id="{{::$ctrl.name}}" ng-disabled="$ctrl.disabled" class="form-control input-sm {{::$ctrl.classes}}" ng-class="[{datepicker: $ctrl.time!=true},{datetimepicker: $ctrl.time==true}]" ng-model="$ctrl.model" external-update="$ctrl.updateInput"> </div> </div>` }) .component('daterangebox', { bindings: { size: '@@?', name: '@@', text: '@@', model: '=', classes: '@@?', disabled: '=?', time: '<?' }, controller: function() { if (!this.model || typeof this.model !== 'object') { this.model = {}; } if (!this.size) { this.col1 = '6'; this.col2 = '6'; } else { var size = parseInt(this.size); this.col1 = size.toString(); this.col2 = (12 - size).toString(); } this.updateInput = function(element, setter) { element.on("dp.change", function() { setter(element.val()); }); } }, template: String.raw` <div class="form-group"> <label ng-if="$ctrl.col1!='0'" for="{{::$ctrl.name}}" class="col-md-{{::$ctrl.col1}} control-label">{{::$ctrl.text}}</label> <div class="col-md-{{::$ctrl.col2}}"> <div class="input-group"> <input type="text" id="{{::$ctrl.name}}" ng-disabled="$ctrl.disabled" class="form-control input-sm {{::$ctrl.classes}}" ng-class="[{datepicker: $ctrl.time!=true},{datetimepicker: $ctrl.time==true}]" ng-model="$ctrl.model.start" external-update="$ctrl.updateInput"> <span class="input-group-addon input-sm">-</span> <input type="text" ng-disabled="$ctrl.disabled" class="form-control input-sm {{::$ctrl.classes}}" ng-class="[{datepicker: $ctrl.time!=true},{datetimepicker: $ctrl.time==true}]" ng-model="$ctrl.model.end" external-update="$ctrl.updateInput"> </div> </div> </div>` })
source share