在 AngularJS 中使用 ng-change 时如何将 'time' 的输入类型转换为字符串?
How to convert an input type of 'time' into a string while using ng-change in AngularJS?
我想将时间输入转换为字符串或时间戳,以便 Firebase 支持它。
例如,下面的代码将不起作用,因为输入有时间类型。
HTML
<html ng-app='app'>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.0.2/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script>
</head>
<body ng-controller="ctrl">
<input ng-change="data.$save()" ng-model="data.text">
{{data.text}}
<br>
<input type='time' ng-change="data.$save()" ng-model="data.time"></input>
{{data.time}}
</body>
AngularJS
var app = angular.module("app", ["firebase"]);
app.controller("ctrl", ["$scope","$firebase", function($scope,$firebase)
{
var ref = new Firebase('https://welcome.firebaseIO.com/Data/RandomUser');
$scope.data = $firebase(ref).$asObject();
}]);
参见codpen here。
我尝试用以下方法转换时间的输入,但没有成功:
尝试在 data.time
的末尾添加 .toString()
<input type='time' ng-change="data.$save()" ng-model="data.time.toString()">
尝试在数据末尾添加 .toString()。$save()
<input type='time' ng-change="data.$save().toString()" ng-model="data.time">
我还查看了 ng-change documentation,它表明表达式会立即求值,所以 ng-change 可能无法做到这一点。
日期操作作为 an example in the AngularFire API docs (search for "To illustrate, let’s create a factory that creates Widget instances, and transforms dates:") and, as a topic, manipulating data is covered in the section on extending services. There is also a fiddle 演示日期操作给出。
这是来自 fiddle 的示例:
/**
* Add a factory object which parses dates
*/
app.factory('DateFactory', function ($firebaseObject) {
return $firebaseObject.$extend({
/**
* Called each time there is an update from the server
* to update our data
*/
$$updated: function (snap) {
// call the super
var changed = $firebaseObject.prototype
.$$updated.apply(this, arguments);
// manipulate the date
if( changed ) {
this.date = new Date(this.date||0);
}
// inform the sync manager that it changed
return changed;
},
/**
* Used when our data is saved back to the server
* to convert our dates back to JSON
*/
toJSON: function() {
return angular.extend({}, this, {
// revert Date objects to json data
date: this.date? this.date.getTime() : null
});
}
});
});
请注意,一般而言,数字日期比字符串更有用(更易于解析、排序、查询等),并且这些示例使用了这种高级存储策略。将其转换为字符串应该是不言自明的。
我想将时间输入转换为字符串或时间戳,以便 Firebase 支持它。
例如,下面的代码将不起作用,因为输入有时间类型。
HTML
<html ng-app='app'>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.0.2/firebase.js"></script>
<script src="https://cdn.firebase.com/libs/angularfire/0.9.0/angularfire.min.js"></script>
</head>
<body ng-controller="ctrl">
<input ng-change="data.$save()" ng-model="data.text">
{{data.text}}
<br>
<input type='time' ng-change="data.$save()" ng-model="data.time"></input>
{{data.time}}
</body>
AngularJS
var app = angular.module("app", ["firebase"]);
app.controller("ctrl", ["$scope","$firebase", function($scope,$firebase)
{
var ref = new Firebase('https://welcome.firebaseIO.com/Data/RandomUser');
$scope.data = $firebase(ref).$asObject();
}]);
参见codpen here。
我尝试用以下方法转换时间的输入,但没有成功:
尝试在 data.time
的末尾添加 .toString()<input type='time' ng-change="data.$save()" ng-model="data.time.toString()">
尝试在数据末尾添加 .toString()。$save()
<input type='time' ng-change="data.$save().toString()" ng-model="data.time">
我还查看了 ng-change documentation,它表明表达式会立即求值,所以 ng-change 可能无法做到这一点。
日期操作作为 an example in the AngularFire API docs (search for "To illustrate, let’s create a factory that creates Widget instances, and transforms dates:") and, as a topic, manipulating data is covered in the section on extending services. There is also a fiddle 演示日期操作给出。
这是来自 fiddle 的示例:
/**
* Add a factory object which parses dates
*/
app.factory('DateFactory', function ($firebaseObject) {
return $firebaseObject.$extend({
/**
* Called each time there is an update from the server
* to update our data
*/
$$updated: function (snap) {
// call the super
var changed = $firebaseObject.prototype
.$$updated.apply(this, arguments);
// manipulate the date
if( changed ) {
this.date = new Date(this.date||0);
}
// inform the sync manager that it changed
return changed;
},
/**
* Used when our data is saved back to the server
* to convert our dates back to JSON
*/
toJSON: function() {
return angular.extend({}, this, {
// revert Date objects to json data
date: this.date? this.date.getTime() : null
});
}
});
});
请注意,一般而言,数字日期比字符串更有用(更易于解析、排序、查询等),并且这些示例使用了这种高级存储策略。将其转换为字符串应该是不言自明的。