How to implement DOM manipulation in Angular?

Recently, I was instructed to take over and clean the Angular project, which has already been completed and launched. This is my first time using Angular.

Everything I've read so far on Angular ...

... Everyone seems to be talking about the architecture of the Angular application. First of all, these are:

Controllers should never perform DOM manipulations or hold DOM selectors; that when directives and use of the ng model come in.

However, the project that was assigned to me completely ignores this. For example, an excerpt from MenuController:

(function() { app.controller('MenuController', function($scope) { ... $scope.openMenu = function () { $('.off-canvas-wrap').addClass('offcanvas-overlap-right'); }; ... }); }()); 

Should I move this code (and many other code) into directives? Or should I follow the pattern that the application has already installed and continues to manipulate the DOM in controllers?

+6
source share
2 answers

Yes. The code that works with the DOM does not work in the angular controller and belongs to the directive.

Another advantage of using angular is that there are many good ready-made directives. Therefore, if you are going to reorganize things in any way, you can see if you can speed things up with other directives.

However, that sounds like a lot of work. You want to tell who pays for it and discuss the pros and cons of refactoring with them.

+5
source
 $scope.openMenu = function () { $scope.newClass = 'offcanvas-overlap-right'; }; 

and in partial

 <div class="off-canvas-wrap" ng-class="newClass"></div> 
+2
source

Source: https://habr.com/ru/post/985432/


All Articles