Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Consider the piece of code below:

part of html file:

<body ng-app>
    <div ng-controller="MainCtrl">{{name}}</div>
</body>

part of js file:

function MainCtrl($scope) {
  $scope.name = "John";
}

I always put my controllers in some module, whose name I define in ng-app. How it works when I don't define any module ?

See Question&Answers more detail:os

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

1 Answer

Angular, has this "auto-discover" feature, that allows it to look up controllers by their name if they are defined on the global scope.

This feature is mainly intended for quick demos/prototypes/proof-of-concept snippets and not real-world applications.


From Angular's Developer Guide:

NOTE: Although Angular allows you to create Controller functions in the global scope, this is not recommended. In a real application you should use the .controller method of your Angular Module for your application [...]


Regarding the empty ngApp, if there is no name specified, it only means that there is no module to assign controllers, directives, services to, but other than that everything works fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...