【问题标题】:Angular JS Json Datetime filter use from inputAngular JS Json Datetime过滤器从输入中使用
【发布时间】:2018-04-26 14:58:14
【问题描述】:

这是我的过滤功能(来自 json 日期转换):

app.filter("mydate", function () {
var re = /\/Date\(([0-9]*)\)\//;
return function (x) {
    var m = x.match(re);
    if (m) return new Date(parseInt(m[1]));
    else return null;
};

});

如果我在这里使用它没问题:

{{k.StartDate | mydate | date: 'yyyy-MM-dd HH:mm'}}

我想在 input 中使用这个过滤器,但是我收到了这个错误:

error

我使用这样的过滤器

<input class="w-100" datetime="yyyy-MM-dd" ng-model="ApplyToProgress.CurrentWork.StartDate | mydate | date: 'yyyy-MM-dd HH:mm'" type="text">

【问题讨论】:

  • 是的。您无法过滤模型。
  • you can't filter an ng-model。我认为您想要的解决方案是自定义指令而不是过滤器。在指令中应用更改,可能使用 ngModel.$parsers.push(function (input) { ...
  • 尝试使用

标签: c# .net angularjs json datetime


【解决方案1】:

您不能在 ng-model 上使用过滤器。我建议您使用 ng-change 观察变化并在控制器上应用过滤器,如下所示:

<input class="w-100" datetime="yyyy-MM-dd" ng-model="ApplyToProgress.CurrentWork.StartDate" ng-change="applyFilter()" type="text">

在控制器中:

$scope.applyFilter = function(){
    $scope.ApplyToProgress.CurrentWork.StartDate = $filter('mydate')($scope.ApplyToProgress.CurrentWork.StartDate);
    $scope.ApplyToProgress.CurrentWork.StartDate = $filter('date')($scope.ApplyToProgress.CurrentWork.StartDate, 'yyyy-MM-dd HH:mm');
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2017-01-21
    • 2016-12-26
    • 2017-04-01
    • 2016-10-22
    相关资源
    最近更新 更多