2016-08-05 12 views
1

Dies ist mein Code:ng, wenn nicht mit getDay funktioniert()

<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> 

Controller:

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

Das Problem ist, dass, wenn ich die zweite ng-if (Dienstag) einfügen Der richtige Tag wird nicht mehr angezeigt. Verpasse ich etwas?

+0

Die zweite ng-wenn? Wo ist der zweite ng-if? Was ist der Wert von "d"? – Weedoze

+0

Es funktioniert für mich, hast du nicht vergessen, getDate irgendwo anzurufen? Kannst du Code hinzufügen, der dazu gehört? Dieses einzige Stück Code ist in Arbeit. – Walfrat

+1

Sie haben nur vergessen, die Funktion 'getDay()' – Weedoze

Antwort

1

Sehen Sie diese Funktion, die Sie hier definiert haben:

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

Es ist nur zu einer $scope Variable d zuweisen Wert. Wenn das alles ist, was Sie tun wollen, warum tun Sie das nicht direkt, indem Sie diese Variablen deklarieren? Gefällt Ihnen dieses

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>

2

seine Arbeits ..

var app = angular.module('plunker', []); 
 

 
app.controller('MainCtrl', function($scope) { 
 
    var n = new Date(); 
 
    console.log(n.getDay()); 
 
     $scope.d = n.getDay(); 
 
});
<script data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js" data-require="[email protected]"></script> 
 
    
 
<body ng-app="plunker" ng-controller="MainCtrl"> 
 
    <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> 
 
    </body>