var app = angular.module('app', []); app.controller(); app.factory();
angular .module('app', []) .controller() .factory();
controller
, factory
, directive
, service
, etc. There are also many different syntaxes for modules when it comes to DI and code formatting. Use the definition of named functions to later transfer their names to the appropriate methods. This way gives you more opportunity when tracing the stack, since functions are not anonymous (of course, you can just start using named functions instead of anonymous ones, but this way is more readable). var app = angular.module('app', []); app.controller('MyCtrl', function () { });
function MainCtrl () { } angular .module('app', []) .controller('MainCtrl', MainCtrl);
angular.module('app', [])
. Then, if you need to access this module (for example, in other files), use the getter syntax: angular.module('app')
. (function () { angular.module('app', []); // MainCtrl.js function MainCtrl () { } angular .module('app') .controller('MainCtrl', MainCtrl); // AnotherCtrl.js function AnotherCtrl () { } angular .module('app') .controller('AnotherCtrl', AnotherCtrl); // ... })();
controller
syntax, they have controllerAs
syntax. Use it precisely because, in addition to being able to reference the controller instance, this method makes the scopes nested. <div ng-controller="MainCtrl"> {{ someObject }} </div>
<div ng-controller="MainCtrl as main"> {{ main.someObject }} </div>
ng-controller
attribute in many respects limits the use of this view (view) only in conjunction with the specified controller. And although rarely, there are still situations where the same representation can be used with different controllers. For more flexibility in this matter, use a router to connect the view with the controller. <!-- main.html --> <div> {{ main.someObject }} </div> <!-- main.html --> <script> // ... function config ($routeProvider) { $routeProvider .when('/', { templateUrl: 'views/main.html', controller: 'MainCtrl', controllerAs: 'main' }); } angular .module('app') .config(config); //... </script>
$parent
when you need to access any of the parent controllers, with this approach, we simply write the controllerAs
value, in our case main
. It is clear that due to this method, we also avoid calls like $parent.$parent
.controllerAs
syntax implies the use of the this
in the controller code instead of $scope
. When using controllerAs
, the controller is actually tied to $scope
, which, in fact, gives such a degree of separation. function MainCtrl ($scope) { $scope.someObject = {}; $scope.doSomething = function () { }; } angular .module('app') .controller('MainCtrl', MainCtrl);
prototype
to create controller classes, but this will quickly pollute the code, because each DI provider needs a corresponding reference in the constructor. function MainCtrl ($scope) { this.someObject = {}; this._$scope = $scope; } MainCtrl.prototype.doSomething = function () { // use this._$scope }; angular .module('app') .controller('MainCtrl', MainCtrl);
prototype
but do not know why, then this is bad. If you use prototype
to isolate from other controllers, this is good. And for the general case, the use of prototype
may simply be redundant. function MainCtrl () { this.someObject = {}; this.doSomething = function () { }; } angular .module('app') .controller('MainCtrl', MainCtrl);
function MainCtrl () { this.doSomething = function () { }; } angular .module('app') .controller('MainCtrl', MainCtrl);
function MainCtrl (SomeService) { this.doSomething = SomeService.doSomething; } angular .module('app') .controller('MainCtrl', MainCtrl);
this
, we make out the code of functions in accordance with everything else. function SomeService () { this.someMethod = function () { }; } angular .module('app') .service('SomeService', SomeService);
function AnotherService () { var someValue = ''; var someMethod = function () { }; return { someValue: someValue, someMethod: someMethod }; } angular .module('app') .factory('AnotherService', AnotherService);
function AnotherService () { var AnotherService = {}; AnotherService.someValue = ''; AnotherService.someMethod = function () { }; return AnotherService; } angular .module('app') .factory('AnotherService', AnotherService);
Source: https://habr.com/ru/post/235873/
All Articles