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

    爲什麼AngularJS在select中包含空選項?我的默認值是空的? 打擊是我的代碼:爲什麼我的指令不能設置初始值

    .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-initlink函數調用,所以嘗試此解決方案:

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

    或者這一個:

    .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]; 
         } 
        }}) 
    
    相關問題