【问题标题】:I am facing issue on data binding with AngularJS我在使用 AngularJS 进行数据绑定时遇到问题
【发布时间】:2017-01-02 10:59:16
【问题描述】:

我是 Angular JS 的新手。我在使用 AngularJS 进行数据绑定时遇到问题。它适用于字符串,但不适用于日期时间。我正在将 ASP.net MVC 与 Angular JS 和 jQuery UI 一起用于日期选择器。

  1. 数据来自数据库(控制器),采用 JSON 格式。
  2. 通过使用 Angular JS 进行数据绑定,它会自动设置为输入框。
  3. 在输入框中附加了 jQuery UI 日期选择器。

它适用于除日期时间以外的所有字段。

案例 1:如果我设置 input type="date" 然后输入日期字段为空。

案例 2:如果我设置 input type="string" 然后它在输入中显示值,即。 /日期(1481481000000)/

这是我的控制器代码:

    // GET: ProductMaster/Details/5
    public ActionResult Details(int PrimaryKey)
    {
        try
        {
            using (LOAN_APPRAISAL_TESTEntities db = new LOAN_APPRAISAL_TESTEntities())
            {

                var v = (from v1 in db.m_ProductMaster where v1.ProductID == PrimaryKey select v1).First();
                ProductMasterModel product = new ProductMasterModel();
                v.MapToModelObject(product);
                return Json(product, JsonRequestBehavior.AllowGet);
            }
        }
        catch (Exception e1)
        {
            return Json(new ResultModel() { Result = tssiplResultType.tssiplError, Message = e1.Message, Data = null }, JsonRequestBehavior.AllowGet);
        }
    }

这是我的 Angular JS 代码

$.ajax({
        url: '/ProductMaster/Details',
        type: 'GET',
        cache: false,
        async: false,
        data: { PrimaryKey: ProductID },
        contentType: 'application/json; charset=utf-8',
        error: function (a, b, c) {
            HideBusy();
            alert(a.responseText);
        },
        success: function (a, b, c) {
            HideBusy();
            debugger;
            $scope.ProductMasterModel = a; // this is my Angular Model

            //debugger;
              //debugger;
            $('#divEdit').modal('show');
        }
    });

这是我的 HTML

                        <div class='input-group date .form_datetime'>
                            <label>Valid From</label>
                            <input class="form-control" ControlType="Date" type="date" 
                                   min="1900-01-01"
                                   max="2500-01-01"
                                   ng-model="ProductMasterModel.ValidFrom" />

                        </div>


                        <label>Valid To</label>
                        <input class="form-control" ControlType="Date" type="text" 
                                   min="1900-01-01"
                                   max="2500-01-01"
                                   ng-model="ProductMasterModel.ValidTo"/>

注意: 我不想使用 input type="date" 因为 HTML5 日期选择器不适用于所有浏览器,因此更喜欢使用 jQuery UI 日期选择器。

我浏览了几个链接,每个链接都显示格式化日期并以跨度显示。

【问题讨论】:

  • 如果你能详细说明你的评论,那将是很重要的
  • 您的服务器端日期时间格式需要被解析为 javascript Date() 可用的格式。这可以很容易地在网络上搜索,并且有多种方法可以做到这一点
  • 嗯,这是 Angular JS 代码,请参见 ng-model。我所了解的是数据绑定适用于 JSON,它应该与 html 绑定。现在我的 json 数据来自我的 ajax 调用......其次,它适用于字符串和整数值,但不适用于日期时间。我也知道数据绑定发生在内部,我该怎么做?
  • 也永远不要使用 async: false, 。这是一种糟糕的做法,已被浏览器弃用,您必须在控制台中忽略它们的弃用警告
  • 你为什么使用 jQuery 来调用 Ajax 而不是 Angular 的 '$http' 函数?

标签: jquery asp.net angularjs asp.net-mvc jquery-ui-datepicker


【解决方案1】:

在您的领域中使用角度 $filter

 <input class="form-control" ControlType="Date" type="text" min="1900-01-01"
                                   max="2500-01-01"
                                   ng-model="ProductMasterModel.ValidFrom | date" />

【讨论】:

  • 那么你对 type="date" 有什么看法,应该是 type="text" 或 type="date"
  • 你的愿望。自定义过滤器可以返回您想要使用的任何数据类型
  • 不,格式不适用于 type="date" 和 type="text"
  • 使用自定义过滤器即可。如果您想要 type=text,则使过滤器返回字符串值,否则为日期时间值
  • 在 ng-model 中分配标准日期过滤器后,出现错误“错误:[ngModel:nonassign] errors.angularjs.org/1.3.15/ngModel/…”。你能告诉我哪里错了吗
猜你喜欢
  • 2020-11-09
  • 2011-07-12
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多