ngModel不适用于Angular UI bootstrap中的输入[type =" date"]

时间:2015-08-10 19:42:13

标签: angularjs datepicker angular-ui-bootstrap angular-ngmodel angular-ui-datepicker

使用<input type="text"时,角度ui引导日期选择器可以正常使用所有功能,但遗憾的是ng-model在使用ng-click="editExpense(expense);

<input type="text"不起作用

演示:http://codepen.io/athimannil/pen/yNwjmW

&#13;
&#13;
var app = angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
app.controller('DatepickerDemoCtrl', function($scope) {
  $scope.newexpense = {};
  $scope.expenses = [{
    "id": 2,
    "name": "Declan Ayala",
    "amount": "29.18",
    "date": "2015-08-19T18:17:35.647Z",
    "description": "Integer eu lacus. Quisque imperdiet, erat nonummy ultricies ornare, elit elit fermentum risus, at fringilla purus mauris a nunc. In at pede. Cras"
  }, {
    "id": 3,
    "name": "Ivana Bell",
    "amount": "88.53",
    "date": "11/20/2014",
    "description": "Etiam imperdiet dictum magna. Ut tincidunt orci quis lectus. Nullam suscipit, est ac facilisis facilisis, magna tellus faucibus leo, in lobortis tellus justo sit amet nulla."
  }, {
    "id": 4,
    "name": "Brennan Noel",
    "amount": "17.76",
    "date": "02/06/2015",
    "description": "velit eu sem. Pellentesque ut ipsum ac mi eleifend egestas. Sed pharetra, felis"
  }, {
    "id": 5,
    "name": "Thomas Tran",
    "amount": "29.59",
    "date": "08/20/2014",
    "description": "nisl sem, consequat nec, mollis vitae, posuere at, velit. Cras lorem lorem, luctus ut, pellentesque eget, dictum placerat, augue. Sed molestie. Sed id risus quis diam luctus lobortis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Mauris ut quam vel sapien imperdiet ornare."
  }, {
    "id": 6,
    "name": "Tucker Bolton",
    "amount": "66.69",
    "date": "12/09/2014",
    "description": "et risus. Quisque libero lacus, varius et, euismod et, commodo at, libero. Morbi accumsan laoreet ipsum. Curabitur consequat, lectus sit amet luctus vulputate, nisi sem semper erat, in consectetuer ipsum nunc id enim. Curabitur massa. Vestibulum accumsan neque et nunc. Quisque ornare tortor at risus. Nunc ac sem ut dolor dapibus gravida. Aliquam tincidunt, nunc ac mattis ornare, lectus ante dictum mi, ac mattis velit justo nec ante. Maecenas mi felis, adipiscing fringilla, porttitor vulputate, posuere vulputate, lacus. Cras interdum."
  }, {
    "id": 7,
    "name": "Medge Mullen",
    "amount": "23.78",
    "date": "10/17/2015",
    "description": "dictum mi, ac mattis velit justo nec ante. Maecenas mi felis, adipiscing fringilla, porttitor vulputate, posuere vulputate, lacus. Cras interdum. Nunc sollicitudin commodo ipsum. Suspendisse non leo. Vivamus nibh dolor, nonummy ac, feugiat non, lobortis quis, pede. Suspendisse dui. Fusce diam nunc, ullamcorper eu, euismod ac, fermentum vel, mauris. Integer sem elit, pharetra ut, pharetra sed, hendrerit a, arcu. Sed et"
  }, {
    "id": 8,
    "name": "Morgan Merritt",
    "amount": "54.09",
    "date": "04/10/2016",
    "description": "Nulla facilisis. Suspendisse commodo tincidunt nibh. Phasellus nulla. Integer vulputate, risus a ultricies adipiscing, enim mi tempor lorem, eget mollis lectus pede et risus. Quisque libero lacus, varius et, euismod et,"
  }, {
    "id": 9,
    "name": "Janna Casey",
    "amount": "22.61",
    "date": "08/05/2015",
    "description": "eros turpis non enim. Mauris quis turpis vitae purus gravida sagittis. Duis gravida. Praesent eu nulla at sem molestie sodales. Mauris blandit enim consequat purus. Maecenas libero est, congue a, aliquet vel, vulputate eu, odio. Phasellus at augue id ante dictum"
  }, {
    "id": 10,
    "name": "Buffy Best",
    "amount": "59.98",
    "date": "05/06/2015",
    "description": "lacus vestibulum lorem, sit amet ultricies sem magna nec quam. Curabitur vel lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec dignissim magna a tortor. Nunc commodo auctor velit. Aliquam nisl. Nulla eu neque pellentesque massa lobortis ultrices. Vivamus rhoncus. Donec est. Nunc ullamcorper, velit in aliquet lobortis, nisi nibh lacinia orci, consectetuer euismod est arcu ac orci. Ut semper pretium neque. Morbi quis urna."
  }, {
    "id": 11,
    "name": "Brielle Newman",
    "amount": "52.26",
    "date": "04/16/2016",
    "description": "placerat, augue. Sed molestie. Sed id risus quis diam luctus lobortis."
  }, {
    "id": 12,
    "name": "Julie James",
    "amount": "6.38",
    "date": "08/15/2014",
    "description": "placerat, orci lacus vestibulum lorem, sit amet ultricies sem magna nec quam. Curabitur vel lectus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec dignissim magna a tortor. Nunc commodo auctor velit. Aliquam nisl. Nulla eu neque"
  }, {
    "id": 13,
    "name": "Shaine Barnett",
    "amount": "0.04",
    "date": "09/29/2014",
    "description": "laoreet, libero et tristique pellentesque, tellus sem mollis dui, in sodales elit erat vitae risus. Duis a mi fringilla mi lacinia mattis. Integer eu lacus. Quisque imperdiet, erat nonummy ultricies ornare, elit elit fermentum risus, at fringilla purus mauris a nunc. In at pede. Cras vulputate velit eu sem. Pellentesque ut ipsum ac mi eleifend egestas. Sed"
  }, {
    "id": 14,
    "name": "Genevieve Underwood",
    "amount": "50.71",
    "date": "09/24/2015",
    "description": "consequat auctor, nunc nulla vulputate dui, nec tempus mauris erat eget ipsum. Suspendisse sagittis. Nullam vitae diam. Proin dolor. Nulla semper"
  }, {
    "id": 15,
    "name": "Adam Villarreal",
    "amount": "18.48",
    "date": "04/06/2016",
    "description": "nisl. Maecenas malesuada fringilla est. Mauris eu turpis. Nulla aliquet. Proin velit. Sed malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas, urna justo faucibus lectus, a sollicitudin orci sem eget massa. Suspendisse eleifend. Cras sed leo. Cras vehicula aliquet libero. Integer in magna. Phasellus dolor elit, pellentesque a, facilisis non, bibendum sed, est. Nunc laoreet lectus quis massa. Mauris"
  }];
  $scope.editExpense = function(thisExpense) {
    $scope.newexpense = angular.copy(thisExpense);
  };
  $scope.cancelExpense = function() {
    $scope.newexpense = {};
  };
});
&#13;
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.13.3.js"></script>
<div ng-app="ui.bootstrap.demo" ng-controller="DatepickerDemoCtrl" class="container">
  <div class="row">
    <form class="form-horizontal">
      <h3>New Expense</h3>
      <br>
      <div class="form-group">
        <label for="" class="col-sm-2 control-label">Name</label>
        <div class="col-sm-3">
          <input type="text" class="form-control" ng-model="newexpense.name" placeholder="Name">
        </div>
        <label for="" class="col-sm-1 control-label">Amount</label>
        <div class="col-sm-3">
          <input type="text" class="form-control" ng-model="newexpense.amount" placeholder="Description">
        </div>
        <label for="" class="col-sm-1 control-label">Description</label>
        <div class="col-sm-2">
          <input type="text" class="form-control" ng-model="newexpense.description" placeholder="Description">
        </div>
      </div>
      <div class="form-group">
        <label for="" class="col-sm-2 control-label">input="text"</label>
        <div class="col-sm-3">
          <div class="input-group">
            <input type="text" class="form-control" datepicker-popup ng-model="newexpense.date" is-open="expensepicker" show-button-bar="false">
            <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="expensepicker=true"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
          </div>
        </div>
        <label for="" class="col-sm-2 control-label">input="date"</label>
        <div class="col-sm-3">
          <div class="input-group">
            <input type="date" class="form-control" datepicker-popup ng-model="newexpense.date" is-open="expensepicker" show-button-bar="false">
            <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="expensepicker=true"><i class="glyphicon glyphicon-calendar"></i></button>
              </span>
          </div>
        </div>
      </div>
      <div class="form-group">
        <div class="col-sm-12 text-right">
          <button type="button" class="btn btn-default" ng-click="cancelExpense();">Cancel</button>
          <button type="submit" class="btn btn-primary">Save</button>
        </div>
      </div>
      <hr>
    </form>
    <table class="table table-hover table-responsive">
      <thead>
        <tr>
          <th>Name</th>
          <th>Amount</th>
          <th>Date</th>
          <th>Edit</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="expense in expenses">
          <td>{{expense.name}}</td>
          <td>{{expense.amount}}</td>
          <td>{{expense.date}}</td>
          <td>
            <div class="btn-group btn-group-xs" role="group" ng-init="editmode=false">
              <button type="button" class="btn btn-default" ng-click="editExpense(expense);">
                <i class="glyphicon glyphicon-pencil"></i>
              </button>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

$scope.newexpense.date这是一个字符串;您需要将其转换为日期对象;

$scope.editExpense = function (thisExpense) {
    $scope.newexpense = angular.copy(thisExpense);
    $scope.newexpense.date = new Date($scope.newexpense.date.substr(0, 10));
};

您是否有机会在项目中使用一种日期格式?它会解决很多问题;

我希望我已正确理解你的问题。