为什么最新版本的angular不支持全局控制器功能? [英] Why doesn't the latest version of angular support global controller functions?

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

问题描述

使用 AngularJs 新版本 1.3.0,不要使用 1.2.9 旧版本.新版本有什么新内容?

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

<脚本>函数 MyController($scope) {$scope.author = {'name': 'Nagy Dávid','title': '演示',}}</html>

解决方案

angular v1.3.0-beta.15 有一个重大变化,因此默认情况下,angular 将不再看起来用于窗口上的控制器.有关详细信息,请参阅 3f2232b5.

除了简单的demo,使用globals是没有帮助的对于控制器构造函数.这为 `$controllerProvider` 添加了一个新方法重新启用旧行为,但默认禁用此功能.突破性变化:`$controller` 将不再在 `window` 上寻找控制器.查看控制器的窗口"的旧行为最初是打算用于示例、演示和玩具应用程序.我们发现允许全局控制器函数鼓励不良做法,因此我们决定通过以下方式禁用此行为默认.要迁移,请使用模块注册您的控制器而不是公开它们作为全局变量:

因此,为了让您的示例在不创建自己的模块的情况下工作(虽然不推荐),您可以将此代码添加到底部的脚本标记中:

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

有关工作示例,请参阅下面的 plunker.

示例 plunker: http://plnkr.co/edit/xdlfJRpH8lHzNvqyQ0no?p=预览

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

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

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