年份值未显示在angularjs中

时间:2017-08-02 07:22:59

标签: angularjs angularjs-ng-repeat

我正在尝试在HTML视图中显示年份的值,但年份值根本没有显示。虽然,我的其他值在条件未被激活时显示(如信用卡),但是当我激活它时,年份值未显示,而值来自控制器并显示在控制台中。

以下是视图代码: -

<div class="col-xs-2 innerN" style="width: 14%;">   
                <select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
                    <option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
                    <option ng-repeat="year in page.yearList" value="{{year}}" ng-if="!page.creditcard.isDisabled">{{year}}</option>
                </select>
            </div>

以下是控制器代码: -

$scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
$scope.page.yearList = [];
var d = new Date();
var year = d.getFullYear();
//$scope.page.yearList.push(year);
$scope.page.expyear = year;
console.log("Pawan2", $scope.page.expyear);
for (var i = 0; i < 21; i++) {
    {
        $scope.page.yearList.push(year + i);
    };
}
$scope.years = $scope.page.yearList;
console.log("YearListPawan", $scope.page.yearList);
$scope.page.expmonth = "01";
$scope.monthList = "01";
// watcher
$scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
    //MM-YYYY
    if (typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined") scope.authorize.creditCard
        .expirationDate = null;
    else $scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
    console.log("Pawan", $scope.authorize.creditCard.expirationDate)
}, true);

我希望,我很清楚能够得到答案。 PS:THnx提前寻求帮助。

2 个答案:

答案 0 :(得分:0)

您应该使用 ng-value 而不是值: -

&#13;
&#13;
var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
  $scope.page = {};
  $scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
  $scope.page.yearList = [];
  var d = new Date();
  var year = d.getFullYear();
  //$scope.page.yearList.push(year);
  $scope.page.expyear = year;
  console.log("Pawan2", $scope.page.expyear);
  for (var i = 0; i < 21; i++) {
    {
      $scope.page.yearList.push(year + i);
    };
  }
  $scope.years = $scope.page.yearList;
  
  $scope.authorize = {creditCard: {expirationDate: 'date'}};
  
  console.log("YearListPawan", $scope.page.yearList);
  $scope.page.expmonth = "01";
  $scope.monthList = "01";
  // watcher
  $scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
    //MM-YYYY
    if (typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined")
      $scope.authorize.creditCard.expirationDate = null;
    else
      $scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
    console.log("Pawan", $scope.authorize.creditCard.expirationDate)
  }, true);
});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>


<div ng-app="myApp" ng-controller="myCtrl">
  <div class="col-xs-2 innerN" style="width: 14%;">
    <select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
      <option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
      <option ng-repeat="year in page.yearList" ng-value="year" ng-if="!page.creditcard.isDisabled">{{year}}</option>
    </select>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

以下是我已应用于该方案的解决方案 -

这是控制器代码,几乎没有变化: -

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope) {
$scope.page = {};
$scope.page.monthList = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", ];
$scope.page.yearList = [];
var d = new Date();
var year = d.getFullYear();
var addYears = function () {
    var d = new Date();
    var year = d.getFullYear();
    $scope.page.yearList = [];
    var i = 0;
    for (i = 0; i < 40; i++) {
        $scope.page.yearList.push(year.toString());
        year++;
    }
}
addYears();
$scope.years = $scope.page.yearList;
$scope.page.expyear = d.getFullYear();
$scope.page.expmonth = "01";
$scope.monthList = "01";
// watcher
$scope.$watchGroup(['page.expmonth', 'page.expyear'], function(newValues, oldValues, scope) {
    //MM-YYYY
    if(typeof newValues[1] == "undefined" || typeof newValues[0] == "undefined")
        scope.authorize.creditCard.expirationDate = null;
    else
        $scope.authorize.creditCard.expirationDate = newValues[1] + "-" + newValues[0];
    console.log("Pawan", $scope.authorize.creditCard.expirationDate)
}, true);

以下是视图代码: -

<!DOCTYPE html>
<html><scriptsrc="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div class="col-xs-2 innerN" style="width: 14%;">   
                <select ng-disabled="page.creditcard.isDisabled" class="form-control innerN" placeholder="YYYY" name="ccExpYear" ng-model="page.expyear" required style="border-left:none">
                    <option value="XX" selected ng-if="page.creditcard.isDisabled">XX</option>
                    <option ng-repeat="year in page.yearList" value="{{year}}" ng-if="!page.creditcard.isDisabled">{{year}}</option>
                </select>
            </div>

希望,这对其他人有帮助。