AngularJS 1.0.x的和1.2.x版本之间的差异范围 [英] AngularJS Scope differences between 1.0.x and 1.2.x

查看:127
本文介绍了AngularJS 1.0.x的和1.2.x版本之间的差异范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为下一个稳定AngularJS的发布,我从迁移版本我的应用程序的 1.0.8 1.2

As of the release of the next stable AngularJS, I am migrating my application from version 1.0.8 to 1.2.

在AngularJS 1.0.8 有可能设立类似的指令一个孤立的范围遵循。然后,该指令将使用其自己的测试()函数,而不是控制器的测试()功能。

In AngularJS 1.0.8 it was possible to set up an isolated scope for directives like follow. The directive would then use its own test() function instead of the controller's test() function.

<my-dialog property="something">
    <button ng-click="test()">Load Test</button>
    Check out the test: "{{ testMessage }}"
</my-dialog>

的JavaScript

  .controller('Ctrl', function(scope) {
    scope.test = function () {
       scope.testMessage = 'CTRL Test loaded! Whooops.';
    }
  })

  .directive('myDialog', function() {
    return {
      restrict: 'E',
      scope: {
          property: '='
      },
      link: function(scope) {
          scope.test = function () {
            scope.testMessage = 'Isolated Test loaded. Yes!';
          }
      }
    };

在AngularJS 1.2 此行为不工作了。单击该按钮触发控制器的测试()现在的功能。

In AngularJS 1.2 this behavior doesn't work anymore. Clicking the button fires the controller's test() function now.

看到这个的jsfiddle对比:

See this jsFiddle comparison:

  • Angular 1.0.8
  • Angular 1.2.0

究竟有变化,我怎么能重现昔日的行为?

What exactly has changes and how can I reproduce the old behavior?

我想通了,我可以把指令模板一个额外的HTML文件中,或者编译成一个字符串来得到它的工作(的jsfiddle ),但它似乎是在我的情况太多了,因为模板是固定的,分割在多个部分HTML文件的HTML是一件麻烦事。

I figured out I could place the directives template inside an extra HTML file or compile it as a string to get it working (jsFiddle) but it seems to be too much in my case, as the template is fixed and splitting the HTML over several partial HTML files is a hassle.

@ elclanr的回答时,有没有其他属性共享工作正常。我更新中的jsfiddle通过一些任意的属性。我应该如何现在进行?

@elclanr's answer works fine when there are no other properties to share. I updated the jsFiddle to pass some arbitrary property. How should I proceed now?

推荐答案

看起来这是重大更改的预期结果是:的 github.com/angular/angular.js/commit/... 这是在1.2.0(RC3之后)(的 https://github.com/angular/angular.js/blob/master/CHANGELOG.md - 看到的第一个为打破1.2.0改变 - $编译):

It looks like this is an intended result of the breaking change: github.com/angular/angular.js/commit/… Which was pulled in 1.2.0 (after rc3) (https://github.com/angular/angular.js/blob/master/CHANGELOG.md - see the first breaking change for 1.2.0 - $compile):

修正了与分离范围漏得到处都是到同一个元素上的其他指令。

Fixes issue with isolate scope leaking all over the place into other directives on the same element.

隔离范围,现在只可用于要求它和它的模板分离指令。

一个非分离指令不应该得到相同的元素上分离指令的适用范围隔离,相反,他们将获得在原有范围(这是新创建的分离范围的父范围)。

A non-isolate directive should not get the isolate scope of an isolate directive on the same element,instead they will receive the original scope (which is the parent scope of the newly created isolate scope).

另外,请查阅我们的讨论: github.com/angular/angular.js/issues/ 4889

Also check out this discussion: github.com/angular/angular.js/issues/4889

值得注意的是:该分离范围仅应用于模板,但不是来标记,这不是贡献的指令之前1.2分离作用域具有导致这种泄​​漏的一个错误的该分离的点。范围是,它仅适用于宣布它的指令,而不是其他指令或标记。(从tbosch)

Notably: "The isolate scope is only applied to the template, but not to markup that was not contributed by the directive. Before 1.2 isolate scopes had a bug that caused this kind of leakage. The point of the isolate scope is that it only applies to the directive that declared it, and not to other directives or markup." (from tbosch)

previous为 1.2.0 相同DOM元素上的所有共享同一范围。 所以1.2.0使得与分离范围是如何工作的指示很大的改变。

Previous to 1.2.0 everything on the same DOM element shared the same scope. So 1.2.0 makes a substantial change to how directives with isolate scopes work.

这篇关于AngularJS 1.0.x的和1.2.x版本之间的差异范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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