为什么我的指令不能设置初始值

时间:2017-05-05 05:34:43

标签: angularjs angularjs-directive

为什么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 个答案:

答案 0 :(得分:1)

doGrouping(Arrays.asList("ip", "group"), fakeData) >> {10.0.1.1:user=[{uid=tiger, ip=10.0.1.1, group=user}, {uid=woods, ip=10.0.1.1, group=user}], 10.0.1.0:admin=[{uid=root, ip=10.0.1.0, group=admin}]} doGrouping(Arrays.asList("group"), fakeData) >> {admin=[{uid=root, ip=10.0.1.0, group=admin}], user=[{uid=tiger, ip=10.0.1.1, group=user}, {uid=woods, ip=10.0.1.1, group=user}]} 函数之前调用

ng-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:{
       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>",
  }
})