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]; 
         } 
        }}) 
    
    相关问题