2016-08-05 9 views
1

は、これは私のコードです:ngが-場合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(); 
    } 

の問題は、私は2番目NG-IF(ディーンスターク)を挿入するとき正しい日はもう表示されません。私は何かを逃したのですか?

+0

状の第2 NG-IF? 2番目のng-ifはどこですか? 'd'の値は? – Weedoze

+0

それは私のために働いています、あなたはgetDateをどこかに呼び出すことを忘れなかったのですか?あなたはそれに沿って行くいくつかのコードを追加することはできますか?コードのその唯一の部分はwokringです。 – Walfrat

+1

あなたはちょうど 'getDay()'関数を呼び出すことを忘れてしまった – Weedoze

答えて

1

あなたはここで定義した、この関数を参照してください:それはちょうど、変数d$scopeに値を代入され

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

を。それがあなたがしたいことならば、なぜこれらの変数を宣言することで直接しないのですか?この

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

その作業..

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>

関連する問題