Javascript 使用';选择';要素

Javascript 使用';选择';要素,javascript,angularjs,angularjs-ng-transclude,Javascript,Angularjs,Angularjs Ng Transclude,我有一个有点通用的指令,它包含一个select,通过ajax加载(稍后缓存)一组选项 问题是,我需要根据使用该指令的位置指定一些特定选项。我想我可以排除这样的选择: <select-directive ng-model="model"> <option value="x">custom option</option> </select-directive> link: function(scope, element, attrs, ctr

我有一个有点通用的指令,它包含一个select,通过ajax加载(稍后缓存)一组选项

问题是,我需要根据使用该指令的位置指定一些特定选项。我想我可以排除这样的选择:

<select-directive ng-model="model">
    <option value="x">custom option</option>
</select-directive>
link: function(scope, element, attrs, ctrls, transclude){
    var html = transclude();
    element.find('select').append(html);
}

自定义选项
指令为:

{
            restrict: 'E',
            scope:{
                ngModel: '='
            },
            transclude:true,
            template:[
                '<select class="tipos" ng-model="ngModel">',
                    '<ng-transclude></ng-transclude>',
                    '<option ng-selected="ngModel === item.tipo" ng-repeat="item in ::tiposDoc track by item.tipo" value="{{::item.tipo}}" title="longer description">',
                        'description',
                    '</option>',
                '</select>'
            ].join(''),
            link: function(scope){
                $http.get('viewApp/viewCtrl.php/getTipos',{cache:true})
                    .then(function(response){
                        var resp = response.data;
                        if(!resp.success){
                            console.log(resp.log);
                            alert(res.msg);
                            return false;
                        }
                        scope.tiposDoc = resp.result;
                    });
            }
        }
{
限制:'E',
范围:{
ngModel:“=”
},
是的,
模板:[
'',
'',
'',
“说明”,
'',
''
].加入(“”),
链接:功能(范围){
$http.get('viewApp/viewCtrl.php/getTipos',{cache:true})
.然后(功能(响应){
var resp=响应数据;
如果(!分别成功){
控制台日志(响应日志);
警报(res.msg);
返回false;
}
scope.tiposDoc=resp.result;
});
}
}

但是自定义选项不会显示在select元素中。我错过什么了吗?这是可能的吗?

您可以这样做:

<select-directive ng-model="model">
    <option value="x">custom option</option>
</select-directive>
link: function(scope, element, attrs, ctrls, transclude){
    var html = transclude();
    element.find('select').append(html);
}

显然,
ng include
标记在某种程度上与angular的
select
指令冲突,因此我最终使用的解决方法是包含一个带有
ng transclude
属性的
optgroup
,幸运的是它起了作用:

...
template:[
                '<select class="tipos" ng-model="ngModel">',
                    '<optgroup ng-transclude></optgroup>',
                    '<optgroup>',
                        '<option ng-selected="::ngModel === item.tipo" ng-repeat="item in ::tiposDoc track by item.tipo" value="{{::item.tipo}}" title="{{::item.tipoydesc}}">',
                            '{{::item.tipoydesc}}',
                        '</option>',
                    '</optgroup>',
                '</select>'
            ].join(''),
...
。。。
模板:[
'',
'',
'',
'',
{{::item.tipoydesc}},
'',
'',
''
].加入(“”),
...

Maximus answer也运行得很好,但我无法使某些功能与自定义选项一起工作。

我最终使用了另一种方法,但这是一个完全有效的替代方案,请记住,谢谢!