ng-if不适用于getDay()

奥利维尔

这是我的代码:

  <h2>HEUTE</h2>
  <h2 ng-if="d == 1">Montag: {{cat.montag}}</h2>
  <h2 ng-if="d == 2">Dienstag: {{cat.dienstag}}</h2>
  <h2 ng-if="d == 3">Mittwoch: {{cat.mittwoch}}</h2>
  <h2 ng-if="d == 4">Donnerstag: {{cat.donnerstag}}</h2>
  <h2 ng-if="d == 5">Freitag: {{cat.freitag}}</h2>
  <h2 ng-if="d == 6">Samstag: {{cat.samstag}}</h2>
  <h2 ng-if="d == 0">Sonntag: {{cat.sonntag}}</h2>

控制器:

$scope.getDate = function() {
      var n = new Date();
      $scope.d = n.getDay();
    }

问题是当我插入第二个ng-if(Dienstag)时,不再显示正确的日期。我错过了什么吗?

拉曼·萨哈西(Raman Sahasi)

请参阅您在此处定义的此函数:

myApp.controller("myController", ['$scope', '$http',
  function($scope, $http) {
    $scope.getDate = function() {
      var n = new Date();
      $scope.d = n.getDay();
    }
]);

它只是给$scope变量赋值d如果仅此而已,那么为什么不通过声明这些变量直接进行操作呢?像这样

myApp.controller("myController", ['$scope', '$http',
   function($scope, $http) {
      var n = new Date();
      $scope.d = n.getDay();
   }
]);

<html ng-app="myApp">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>

<body ng-controller="myController">

  <h2>HEUTE</h2>
  <h2 ng-if="d == 1">Montag: {{cat.montag}}</h2>
  <h2 ng-if="d == 2">Dienstag: {{cat.dienstag}}</h2>
  <h2 ng-if="d == 3">Mittwoch: {{cat.mittwoch}}</h2>
  <h2 ng-if="d == 4">Donnerstag: {{cat.donnerstag}}</h2>
  <h2 ng-if="d == 5">Freitag: {{cat.freitag}}</h2>
  <h2 ng-if="d == 6">Samstag: {{cat.samstag}}</h2>
  <h2 ng-if="d == 0">Sonntag: {{cat.sonntag}}</h2>

  <script>
    var myApp = angular.module('myApp', []);

    myApp.controller("myController", ['$scope', '$http',
      function($scope, $http) {
        var n = new Date();
        $scope.d = n.getDay();
      }
    ]);
  </script>
</body>
<html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章