【问题标题】:Date not updated with angularjs日期未使用 angularjs 更新
【发布时间】:2018-01-04 19:51:43
【问题描述】:

我有一个由 ng-repeat 填写的输入类型日期,我希望能够更改日期值但它没有更新:

查看:

// displaying the date value
<tr ng-repeat="sf in selectedFacture">
    <td><input type="date" class="dateCom" ng-model= "sf.dateCom | date" value="{{sf.dateCom}}"/></td>
    <td><input type="date" class="dateRec" ng-model= "sf.dateRec | date" value="{{sf.dateRec}}"/></td>
    <td ng-click="updateFacture(sf,id)"> update </td>
</tr>
JS:
//sending the date to server side and update the database
$scope.updateFacture=function(sf,id){
          // getting the changed date
                         var editDateCom = sf.dateCom,
                             editDateRec = sf.dateRec;

            var data = {
                "id":id,
                "editDateCom":editDateCom,
                "editDateRec":editDateRec
            };
            var options = {
            type : "get",
            url : url,
            data: data,
            dataType: 'json',
            async : false,
            cache : false,
            success : function(response,status) {
                 $scope.getClients();

            },
            error:function(request,response,error){
            alert("Erro")
            }
        };
        $.ajax(options);
    }

);

其他数据在同一功能中成功更新,但问题是日期,它根本不起作用,控制台记录如下:

错误:ngModel:datefmt 模型不是日期对象

提前致谢

【问题讨论】:

  • 你为什么不使用Angular内置的$http-service?来自$.ajax 的回调不会触发 Angular 的摘要循环,这可能会导致奇怪的问题。它不会解决这个问题,但它可能会解决一些奇怪的行为。
  • 好的,谢谢,但我必须解决这个问题,客户正在等待更新!你知道如何解决这个问题吗?

标签: javascript angularjs date model


【解决方案1】:

您在 ng-model 指令中使用了过滤器,这是错误的

ng-model= "sf.dateCom | date"

正确使用 ng-model= "sf.dateCom"

使用{{}}里面的过滤器

【讨论】:

  • 这对我不起作用! &lt;input type="date" class=" dateCom" ng-model= "sf.dateCom" value="{{sf.dateCom | date}}" /&gt;
  • @AlyAlAmeen 您无法使用 AngularJS 轻松格式化输入字段中的日期。使用date 过滤器进行格式化只是视觉上的——它只有一种方式。所以在输入元素上使用日期过滤器没有任何意义。如果您想要格式化日期,我建议您查看某种日期选择器组件。
  • 我什至从 ng-model 中删除了日期格式,但没有发生任何事情,同样的错误仍然记录在控制台中。 &lt;input type="date" class=" dateCom" ng-model= "sf.dateCom" value="{{sf.dateCom}}" /&gt;
  • 使用不带表达式符号value="sf.dateCom"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 2021-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多