angularjs
  • angularjs-directive
  • 2017-05-05 11 views 0 likes 
    0

    なぜAngularJSにはselectに空のオプションが含まれていますか?私のデフォルト値は空ですか?なぜ私のディレクティブはinitの値を設定できません

    link関数の前に呼び出さ
    .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']; 
        } 
        } 
    }) 
    

    答えて

    1

    ng-initので、このソリューションを試してください: 打撃は私のコードです

    .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']; 
          } 
        } 
    }}) 
    

    か、この1:

    .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>", 
        } 
    }) 
    

    またはこの:

    .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]; 
         } 
        }}) 
    
    関連する問題