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

前端 未结 1 984
清酒与你
清酒与你 2020-12-07 03:05

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 ?


    
             


        
相关标签:
1条回答
  • 2020-12-07 03:36

    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

    0 讨论(0)
提交回复
热议问题