值属性的输入类型=“时间”绑定不会使用角度更新 UI
Input type="time" binding of value attribute doesn't update the UI with angular
我有一个type="time"
的input
标签,如下所示
<input type='time' id='from' ng-model='data.hall.occupancy.timeRange.from' datify value="{{data.hall.occupancy.timeRange.from | propertime}}"/>
过滤器和指令如下
app.directive('datify',function(){
return {
require:'ngModel',
link:function(scope,element,attrs,modelCtrl){
modelCtrl.$parsers.push(function(inputValue){
var times = inputValue.split(":");
var hours = times[0];
var mins = times[1];
var currentDate = new Date();
var outputDate = new Date(currentDate.getFullYear(),currentDate.getMonth(),currentDate.getDate(),hours,mins);
return outputDate.toString();
});
}
};
});
app.filter('propertime',function(){
return function(timeStamp){
if(timeStamp){
var dateObj = new Date(timeStamp.toString());
var date = dateObj.getDate();
var month = dateObj.getMonth()+1;
var year = dateObj.getFullYear();
var hours = (0+ dateObj.getHours().toString()).slice(-2);
var minutes = (0+ dateObj.getMinutes().toString()).slice(-2);
return hours+":"+minutes;//+" "+date+"/"+month+"/"+year;
}
}
});
链接到我的 plnkr : http://plnkr.co/edit/G4G5V62Y70IBvXUXdvQT?p=preview
input
标记的 value
属性在 DOM 中正确更新,但不会影响 UI。但是,更新 UI 中的时间会更新 DOM 中的value
属性。有人可以告诉我这里发生了什么。
注意:我正在使用Chrome(Firefox显示输入类型="time"作为普通文本输入)
我们正在尝试创建一个需要与另一个指令交互的指令/组件(在本例中ng-model
)。
所以我们写了require: 'ngModel',
在我们解析输入$parse(attrs.datify);
之后
.HTML
<input
type='time'
id='from'
datify='data.hall.occupancy.timeRange.from'
ng-model='data.hall.occupancy.timeRange.from'
value="{{data.hall.occupancy.timeRange.from | propertime}}"
/>
命令
app.directive('datify', function ($parse) {
return {
restrict: 'A',
require: 'ngModel',
link: function (scope, element, attrs, modelCtrl) {
var model = $parse(attrs.datify);
scope.$watch(model, function (value) {
if (value.toString().split(":").length == 2) {
backToDate(value)
}
}); // end watch
function backToDate(value) {
var times = value.split(":");
var hours = times[0];
var mins = times[1];
var currentDate = new Date();
var outputDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDay(), hours, mins);
modelCtrl.$setViewValue(outputDate);
}
function validate(value) {
console.log('into validate');
var otherFieldValue = model(scope);
//console.log('validate:', value, otherFieldValue);
var times = value.toString().split(":");
var hours = times[1];
var mins = times[2].split(".")[0];
var currentDate = new Date();
var outputDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDay(), hours, mins);
//console.log("the date:", outputDate.toString());
modelCtrl.$setViewValue(outputDate);
return outputDate;
}
modelCtrl.$formatters.push(validate);
}
};
});
固定演示小提琴
我对角度一无所知,但Chrome,Opera和iOS Safari将支持日期/时间输入类型。 http://caniuse.com/input-datetime
相关文章:
- 如何在范围值更改时使用 angularJs 更新 UI
- 在日期更改后更新Angular UI引导程序日期选择器选项
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- UI网格AngularJS-如何更新UI网格中的列标题
- UI中未更新Knockout ObservableArray
- JQuery UI 对话框在第二次单击时不会更新
- jQuery UI 滑块动画未更新 UI vValue
- Angular/UI路由器-如何在不刷新所有内容的情况下更新URL
- 从jQuery UI滑块更改为'noUiSlider'-用于更新动画进度滑块的函数
- 保持我的 UI 更新,而不会因请求而终止服务器
- JavaScript 函数和 UI 更新
- 为什么 JavaScript 在这里阻止 UI 更新(通过 jquery)
- 在jquery UI更新上设置对象属性
- Kendo UI更新和创建不起作用
- 如何延迟.live()直到UI更新
- 如何在浏览器中强制UI更新,而冗长的javascript计算正在进行中
- Material-UI:更新库后代码不工作
- Jquery美元.when阻塞代码执行和UI更新
- 如何在循环中使用jQuery UI更新变量
- 优化定期 UI 更新