1

我正在使用路由来呈现模板的主体,见下文。模板加载,此时没有问题。

当用户在链接上的呈现模板(/templates/my-details)中单击时,我想触发一个颜色框弹出窗口并使用另一个填充了 my_object 数据的角度模板,该模板在路线中可用。

我编写了一个自定义指令来处理将在点击时触发的代码,获取模板并编译它。然后将其内容传递给颜色框以使用。

我遇到的问题是compiled_template 没有正确呈现。我会假设传递给 $compile(template)(scope) 的范围将包含对象 my_object 以供模板 /templates/my-buy 使用,但它似乎不是因为编译的模板忽略了任何表达式、指令等。 .那个引用my_object。

在我进行路由之前使用此功能并拥有一个更简单的页面,其中包含用于定义 ng-controller 的指令元素。我只能假设我传入的范围不是我正在使用的路由提供程序的范围。“/”。

my_app.config(function($routeProvider) {
        $routeProvider.
          when('/', {
            templateUrl: '/templates/my-details',
            controller: 'MyShowCtrl',
            resolve: {
                my_object: function(myService, settingsService){
                    return dealService.getDeal(settingsService.init().my_guid);
                }
            }   
          }).
          otherwise({
            //redirectTo: '/'
          });
    });

my_app.directive('colorbox', function($compile, $templateCache,$rootScope){
      return {
        link: function(scope, element, attrs){
          element.click('bind', function(){

            // get template from cache process and apply to colorbox
            var template = $templateCache.get('/templates/my-buy');
            var compiled_template = $compile(template)(scope);      

            $.colorbox({
              html: compiled_template,
              width: "600px",
              height: "600px"
            });

          });
        }
      };
    });   
4

1 回答 1

0

您可以直接制作:

$.colorbox({
 innerWidth:350,
 innerHeight:550,
 href: "templates/my-buy"
});
于 2017-04-29T19:12:19.733 回答