2017-05-05 2 views
0

Warum enthält AngularJS eine leere Option in select? und mein Standardwert ist leer? Schlag ist mein Code:Warum kann meine Direktive den Init-Wert nicht setzen

.directive('gettheme',function(){ 
    return { 
    restrict:"AE", 
    template:"<select ng-init='themedata=theme[0]' ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>", 
    link:function(scope, element, attrs){ 
     scope.theme=['macarons', 'infographic']; 
    } 
    } 
}) 

Antwort

1

ng-init vor link Funktion aufgerufen, so versuchen, diese Lösung:

.directive('gettheme',function(){ 
return { 
    restrict:"AE", 
    template:"<select ng-init='themedata=theme[0]' ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>", 
    link:{ 
     pre:function(scope, element, attrs){ 
       scope.theme=['macarons', 'infographic']; 
      } 
    } 
}}) 

oder diese ein:

.directive('gettheme',function(){ 
    return { 
    controller:function($scope){ 
     $scope.theme=['macarons', 'infographic']; 
    }, 
    restrict:"AE", 
    template:"<select ng-init='themedata=theme[0]' ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>", 
    } 
}) 

oder dies:

.directive('gettheme',function(){ 
    return { 
     restrict:"AE", 
     template:"<select ng-model='themedata' ng-options='item for item in theme' class='form-control'></select>", 
     link:function(scope, element, attrs){ 
      scope.theme=['macarons', 'infographic']; 
      scope.themedata=scope.theme[0]; 
     } 
    }}) 
Verwandte Themen