依赖是AngularJS中主要的优点之一,在DI作为一种设计模式被定义在应用程序中作为配置的一部分,如此一来,无需我们手动去创建,当应用程序首次启动时,DI会自动加载模块依赖。
模型类:拿MVC类比,我们通过模型来获取数据,此时我们必须定义模型类,同理在大多数JavaScript客户端框架中也会要求我们创建模型类,但是在AngularJS中这种情况不会发生,在AngularJS中有一个scope对象,此对象用来存储模型类,Scopes被附加到DOM元素上,我们可以通过使用数据属性到scope对象,此对象用来存储模型类,Scopes被附加到DOM元素上,我们可以通过使用数据属性到scope的对象。$rootScope是AngularJS程序中的父Scope。
控制器:在AngularJS中控制器作为视图和model的桥梁,在这个控制器这个区域中我们可以放置一个视图的所有业务逻辑。当应用程序中的业务逻辑被多个控制器所使用时,此时我们应该将业务逻辑放置在AngularJS中的Service(服务)中,当我们需要用到这些业务逻辑时,我们需要在DI的帮助下来获取这些服务。
The purpose of a Scope is to “glue together” the presentation and the business logic of your app.
Every controller has an associated $scope object.
A controller (constructor) function is responsible for setting model properties and functions. This can be done only through $scope. Whatever function or model you apply in View (html file), that is accessed in controller using scope.
Only methods defined on this $scope object are accessible from the HTML/view. Example - from ng-click, filters, etc.
In other words, scope is an “object” that “binds” to DOM element where you apply controller. All child elements can read and modify scope data (unless you modify primitives in new scopes or they’re isolated).