AngularJS templateUrl 与模板 - 隔离范围 [英] AngularJS templateUrl vs template - isolate scope

查看:19
本文介绍了AngularJS templateUrl 与模板 - 隔离范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下指令:

offerListSorters.directive('offersSorter', ['myState', '$templateCache', function (myState, $templateCache){
  return {
    scope: {},
    controller: function($scope, $element, $attrs, $transclude) {
      [...]
    },
    restrict: 'E',
    //templateUrl: 'partials/offersSorterDirective.html',
    template: $templateCache.get('partials/offersSorterDirective.html'),
    replace: true,
    transclude: true
  };
}]);

我使用 Karma + Jasmine 来测试这段代码并且它有效.但是现在如果我切换到 templateUrl(当前已注释掉),它就不起作用了.我创建了一个 simple Plunker 来展示这个问题.当您比较 sorter 和 bsorter 指令时,当我使用 templateUrl 而不是模板时,看起来好像对已编译元素的isolateScope()调用中断了.有什么想法吗?

And I use Karma + Jasmine to test this code and it works. But now if I switch to the templateUrl (currently commented out), it doesn't work. I've created a simple Plunker to show this issue. When you compare sorter and bsorter directives, it looks as if the isolateScope() call on the compiled element breaks when I use templateUrl instead of template. Any ideas?

推荐答案

这是最奇怪的事情,我认为这实际上是一个错误,如果您使用的是 templateUrl,则无法获得孤立的作用域.模板本身已正确加载,但从未加载范围.我已经更新了 plnkr 并添加了一些额外的日志记录,查看控制台,你就会明白我的意思,bsorter 没有得到 ng-isolate-scope 类,范围返回为未定义.

It's the weirdest thing, I think this is actually a bug where if you're using a templateUrl, you don't get an isolated scope. The template itself is loaded correctly, but the scope is never loaded. I've updated the plnkr with some additional logging, checkout out the console and you'll see what I mean, bsorter doesn't get the ng-isolate-scope class and the scope is returned as undefined.

我进一步更新了 plnkr 以在调用编译函数时记录控制台消息.这几乎是我的 javascript/angularJS 知识的限制,但是 bsorter 编译函数被记录为被调用 after 应该返回范围,这与调用 before<的 sorter 编译函数不同/em>.

I've updated the plnkr further to log a console message when the compile function is called. This is pretty much the limit of my javascript/angularJS knowledge, but the bsorter compile function is logged as being called after the scope should be returned, unlike the sorter compile function which is called before.

这篇关于AngularJS templateUrl 与模板 - 隔离范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆