Javascript 在指令中创建解析选项

Javascript 在指令中创建解析选项,javascript,angularjs,twitter-bootstrap,angular-ui-bootstrap,Javascript,Angularjs,Twitter Bootstrap,Angular Ui Bootstrap,我使用角度引导选项卡在一个页面中显示某些部分。为了避免一次加载所有内容,我创建了一个指令,允许在单击时加载选项卡,它还支持为每个选项卡使用单独的控制器 这样,我就有了一个选项卡所在的控制器,每个选项卡都有一个控制器 .directive('template', ['$templateCache', '$compile', '$http', function ($cache, $compile, $http) { return { restrict: 'A',

我使用角度引导选项卡在一个页面中显示某些部分。为了避免一次加载所有内容,我创建了一个指令,允许在单击时加载选项卡,它还支持为每个选项卡使用单独的控制器

这样,我就有了一个选项卡所在的控制器,每个选项卡都有一个控制器

.directive('template', ['$templateCache', '$compile', '$http', function ($cache, $compile, $http) {
    return {
        restrict: 'A',
        replace: false,
        link: function ($scope, element, attrs) {
            var template = attrs['template'];
            var controller = attrs['controller'];

            if (template !== undefined) {
                var tpl = $cache.get(template);

                if (tpl) {
                    compile(tpl);
                } else {
                    $http.get(template).success(function (html) {
                        $cache.put(template, html);
                        compile(html);
                    });
                }
            }

            function compile(html) {
                var e = angular.element(controller === undefined || controller.length === 0 ? html : "<div ng-controller='" + controller + "'>" + html + "</div>");
                var compiled = $compile(e);
                element.replaceWith(e);
                compiled($scope);
            }
        }
    };
}])
.directive('template'、['$templateCache'、'$compile'、'$http',函数($cache、$compile、$http){
返回{
限制:“A”,
替换:false,
链接:函数($scope,element,attrs){
var-template=attrs['template'];
var控制器=属性['controller'];
如果(模板!==未定义){
var tpl=$cache.get(模板);
如果(第三方物流){
编译(tpl);
}否则{
$http.get(模板).success(函数(html){
$cache.put(模板,html);
编译(html);
});
}
}
函数编译(html){
var e=angular.element(controller==undefined | | | controller.length==0?html:“+html+”);
var compiled=$compile(e);
元素。替换为(e);
汇编($范围);
}
}
};
}])
现在我可以这样使用它:

//controller
$scope.data = something;
$scope.tabs = [
        {
            template: 'html-template-url',
            controller: 'controller-name',
            resolve: { //how o implement this?
                data: function () {
                    return $scope.data;
                }
            }
        },

//view
Some data: {{data}}
...
<uib-tab ng-repeat="tab in tabs" select="select(tab)">     
<div ng-if="tab.loaded" template="{{tab.template}}" controller="{{tab.controller}}"></div>
</uib-tab>
...
//控制器
$scope.data=something;
$scope.tabs=[
{
模板:“html模板url”,
控制器:“控制器名称”,
解决方法:{//o如何实现这一点?
数据:函数(){
返回$scope.data;
}
}
},
//看法
一些数据:{{data}}
...
...
问题是我需要将一些信息从主视图传递到选项卡中的部分视图,因为它们是分开的。我可以让它知道如何获取这些信息。我猜是某种解析属性,但我一直在尝试,但没有成功


有人能给我一些建议吗?

您在这里使用路由(即解析)而不是指令有什么特别的原因吗?类似于解析的行为(如在路由中使用)是我想要实现的。我希望能够通过指令将数据传递给视图。