本文介绍了为什么不呢最新版本的角度支持全局控制器的功能呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着AngularJs新版本1.3.0不但是1.2.9旧版本的工作。请告诉我新在新版本中?

 < HTML NG-应用>
    < HEAD>
        <间的charset =UTF-8>
        <标题>< /标题>
        <脚本类型=文/ JavaScript的SRC =angular.min.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
    < /头>
    <身体GT;
        < D​​IV NG控制器=myController的>
            < H1> {{author.name}}< / H1>
            &所述p为H.; {{author.title}}&下; / P>
        < / DIV>        <脚本>            功能myController的($范围){
                $ scope.author = {
                    '名':'纳吉大卫,
                    '标题':'示范',
                }
            }
        < / SCRIPT>
    < /身体GT;
< / HTML>


解决方案

有在角的 V1.3.0-beta.15 的,这样一来,在默认情况下,角将不再看重大更改对窗口控制器。请参见了解更多详情。

通过简单的演示的例外,它是使用全局变量没有帮助
为控制器构造函数。这增加了一种新的方法来`$ controllerProvider`
重新启用旧的行为,但默认情况下禁用此功能。重大更改:
`$ controller`将不再寻求对`window`控制器。
寻找对`window`的控制器旧行为原本打算
为例子,演示和玩具应用的使用。我们发现,让全域控制器
功能鼓励穷人的做法,所以我们决定通过禁止这种行为
默认。要迁移,与模块注册您的控制器,而不是将它们暴露
为全局:

因此​​,为了使您的示例工作,而创建自己的模块(虽然不推荐),你可以在脚本标记在底部添加此code:

  angular.module('NG')。配置(函数($ controllerProvider){
  $ controllerProvider.allowGlobals();
});

有关工作示例,请参见下面一plunker。

示例plunker:

With the AngularJs new version 1.3.0 don't but with 1.2.9 the old version work. Whats the new in the new version ?

<html ng-app>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript" src="jquery.js"></script>
    </head>
    <body>
        <div ng-controller = "MyController">
            <h1>{{author.name}}</h1>
            <p>{{ author.title }}</p>
        </div>

        <script>

            function MyController($scope) {
                $scope.author = {
                    'name': 'Nagy Dávid',
                    'title': 'Demo',
                }
            }
        </script>
    </body>
</html>
解决方案

There is a breaking change in angular v1.3.0-beta.15, so that, by default, angular will no longer look for controllers on window. See 3f2232b5 for more details.

With the exception of simple demos, it is not helpful to use globals
for controller constructors. This adds a new method to `$controllerProvider`
to re-enable the old behavior, but disables this feature by default.

BREAKING CHANGE:
`$controller` will no longer look for controllers on `window`.
The old behavior of looking on `window` for controllers was originally intended
for use in examples, demos, and toy apps. We found that allowing global controller
functions encouraged poor practices, so we resolved to disable this behavior by
default.

To migrate, register your controllers with modules rather than exposing them
as globals:

Therefore, to make your example work without creating your own module (not recommend though), you could add this code in the script tag at the bottom:

angular.module('ng').config(function ($controllerProvider) {
  $controllerProvider.allowGlobals();
});

For a working example, see a plunker below.

Example plunker: http://plnkr.co/edit/xdlfJRpH8lHzNvqyQ0no?p=preview

这篇关于为什么不呢最新版本的角度支持全局控制器的功能呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:58