2016-05-24 4 views
0

Ich habe folgendes in meinem index.htmlAngular - ng-init Umfang Variable vorbei nicht in Sicht

Ich habe folgend in meinem app.js

.controller('MainCtrl', function ($scope) { 
    var today = new Date(); 
    var year = today.getFullYear(); 
    var month = today.getMonth(); 

    $scope.date = { 
     year: year, 
     month: month 
    }; 

    $scope.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] 

    $scope.currentMonth = $scope.months[month - 1]; 

    var years = []; 

    var earliestYear = year - 100; 
    for (var i = earliestYear; i <= year; i++) { 
     years.push(i); 
    } 

    $scope.years = years; 

    $scope.$watchCollection('date', function (date) { 
     $scope.currentDate = new Date(date.year, date.month, 1); 
    }); 

Meine ng- init für "date.year" funktioniert, aber nicht für "currentMonth". Ich weiß jedoch, dass currentMonth mit der Ansicht verbunden ist. Warum erkennt ng-init nicht ng-init = "currentMonth"?

Antwort

2

ng-init ist ein Ausdruck, also müssen Sie currentMonth Ihrem date.month Modell zuweisen.

<select ng-model="date.month" ng-init="date.month=currentMonth" ng-options="month for month in months"> 
</select> 

Die ng-init="date.year" tut eigentlich nichts. Das Modell date.year wurde bereits 2016 zugewiesen, so dass es aussieht, als ob es funktioniert.

Siehe plunker

+0

ah, hab es. Vielen Dank! – Americo