D
D
DarthJS2017-09-04 12:18:33
Angular
DarthJS, 2017-09-04 12:18:33

Why use OOP constructs in an AngularJS application?

I came across one application and it interested me. As far as I understand, OOP principles are used in AngularJS under the hood, but here's why and for what purpose they should be used in service controllers and directives. I would be very grateful if someone could sensibly comment on the following code snippets:
1. Factory:

fcatory('Fac', function () {
 return new Contr();
  function Constr () {
     this.tratata = 123;
   }
  Constr.prototype.fu = function () {}
})

2 Directive:
.directive('myDirective', function () {
  function MyController () {
  this.x = 1231234;
 }
  MyController.prototype.fu = function () {}
 function Template () {
// тут логика конструктора
}
    return {
          restrict: E,
          *****,
          template: new Template();
          controller: function($scope, $element, $attrs) {
          var myController = new MyController($scope, $element, $attrs);
          $scope._myController = myController;
         }
}
})

What is the advantage of using this approach in an AngularJS application, is there any?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question