Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

html - Why doesn't the latest version of angular support global controller functions?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...