这是我的代码:
<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)时,不再显示正确的日期。我错过了什么吗?
答案 0 :(得分: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="angular.js@1.4.x"></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>
答案 1 :(得分:1)
See this function that you've defined here:
myApp.controller("myController", ['$scope', '$http',
function($scope, $http) {
$scope.getDate = function() {
var n = new Date();
$scope.d = n.getDay();
}
]);
It is just assigning value to a $scope
variable d
. If that's all you want to do then why not do it directly by declaring these variables? Like this
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>