我的指令和角度1。 5組件
.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>`
})
其不同的庫,工作原理不同。不重複。 – Rolando
更新了一個答案,以反映boostrap 3的事件,而不是jQuery UI –
我有一個問題,我的viewmodel ng模型屬性不更新時,我從日曆中選擇日期(沒有主動寫入任何文本框) ,並且它仍然與ng-pristine角度指令保持一致,因爲它從不刷新新選定的值。有什麼需要考慮的? – CesarD