2016-10-09 2 views
0

Ich versuche, das Dropdown auf aktuellen Monat, der Oktober ist, aber es funktioniert nicht, irgendwelche Hinweise Jungs?ng-init in angularjs funktionierte nicht für mich

http://plnkr.co/edit/qtPvCy4phNQW6I6IPQyr?p=preview

app.controller('MainCtrl', function($scope) { 
    var d = new Date(); 
     var month = new Array(); 

     var this_year = d.getFullYear(); 

     month[0] = "January " + this_year; 
     month[1] = "February " + this_year; 
     month[2] = "March " + this_year; 
     month[3] = "April " + this_year; 
     month[4] = "May " + this_year; 
     month[5] = "June " + this_year; 
     month[6] = "July " + this_year; 
     month[7] = "August " + this_year; 
     month[8] = "September " + this_year; 
     month[9] = "October " + this_year; 
     month[10] = "November " + this_year; 
     month[11] = "December " + this_year; 

     $scope.months = month; 

     $scope.this_month = month[d.getMonth()] + " " + this_year; // won't work? 
}); 

Antwort

1

Sie können die Standard Monat eingestellt von aktuellen Monat zu model selected_month zuweisen.

$scope.selected_month = $scope.months[d.getMonth()]; 

Plunker

0

Problem gelöst: ngInit für Satz Standardwerte verwendet, und Sie haben versucht, das zu setzen, sondern auf falsche Art finden Sie in diesem Beispiel bitte

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

 
app.controller('MainCtrl', function($scope) { 
 
    var d = new Date(); 
 
    \t \t var month = new Array(); 
 

 
    \t \t var this_year = d.getFullYear(); 
 

 
    \t \t month[0] = "January " + this_year; 
 
    \t \t month[1] = "February " + this_year; 
 
    \t \t month[2] = "March " + this_year; 
 
    \t \t month[3] = "April " + this_year; 
 
    \t \t month[4] = "May " + this_year; 
 
    \t \t month[5] = "June " + this_year; 
 
    \t \t month[6] = "July " + this_year; 
 
    \t \t month[7] = "August " + this_year; 
 
    \t \t month[8] = "September " + this_year; 
 
    \t \t month[9] = "October " + this_year; 
 
    \t \t month[10] = "November " + this_year; 
 
    \t \t month[11] = "December " + this_year; 
 

 
    \t \t $scope.months = month; 
 
    \t \t 
 
    \t $scope.this_month = month[d.getMonth()]; // won't work? 
 
});
<!DOCTYPE html> 
 
<html ng-app="plunker"> 
 

 
    <head> 
 
    <meta charset="utf-8" /> 
 
    <title>AngularJS Plunker</title> 
 
    <script>document.write('<base href="' + document.location + '" />');</script> 
 
    <link rel="stylesheet" href="style.css" /> 
 
    <script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js" data-semver="1.0.7"></script> 
 
    </head> 
 

 
    <body ng-controller="MainCtrl"> 
 
<select style="position: relative;margin-top: -50px;" class="pull-right" ng-model="selected_month" ng-options="m for m in months" ng-init="selected_month = this_month"> 
 
      </select> 
 
      </body> 
 

 
</html>

0

Sie definieren eine Funktion am Controller und eine aufgerufene Methode für ng-init. zum Beispiel

ng-init="this_month()" 

Ändern Sie diese Zeile auf Controller:

$scope.this_month = month[d.getMonth()] + " " + this_year; // won't work? 

zu:

$scope.this_month = //Change this line.... 
     function(){ 
       $scope.selected_month = $scope.months[d.getMonth()]; 
     };