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

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

问题描述

我有以下指令:

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
  };
}]);

和我用噶+茉莉花来测试这个code和它的作品。但现在,如果我切换到templateUrl(目前注释掉),这是行不通的。我创建了一个简单Plunker 以显示此问题。当您比较分拣和bsorter指令,它看起来好像对编译元素断isolateScope()调用当我使用templateUrl代替模板。任何想法?

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-分离范围的类和范围返回不确定的。

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进一步登录控制台消息时编译函数被调用。这是pretty很多我的JavaScript / angularJS知识的限制,但bsorter编译功能被记录为被调用的之后的范围应返回,不像这就是所谓的分拣机编译功能的

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 VS模板 - 隔离范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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