📜 ⬆️ ⬇️

AngularJS best practices

Based on this broadcast.

Instead of the preface, I’ll say that there is such a site yeoman.io , which contains the most popular technologies that automate the development of the frontend (assembly, parameterization of CSS, etc.). Pay attention to him at the beginning of the project.

Using directives


A number of developers believe that the directives are cluttering up HTML, and it is also difficult for them to change old habits. It is worth noting that directives have existed in HTML since time immemorial. These are all familiar form elements: input fields, radio buttons, drop-down lists, and so on. Another thing is that they are made "to be left behind." This problem and solves Angulyar.
')
Compare ordinary checkbox
<input type="checkbox" name="option1" value="a1" checked> 

and checkbox in Angulyar
 <input type="checkbox" ng-model="myModel"> 

What is easier? name/id/class can be omitted, because directives are not bound by name. The initial state is set by the model, so checked does not make sense. But that is not all. The last code can be rewritten and so
 <input type="checkbox" ng-model="myModel" ng-true-value="on" ng-false-value="off" ng-change="change()"> 

I think many at the beginning of acquaintance with HTML wondered why this is not in the standard version, why the checkbox returns on or emptiness, and not true and false , why you can only change the value on and many others.

So, Angulyar simply forces existing interactive elements to lead in an understandable way, unifies the connection of elements with a given model and, on this basis, makes it possible to create any kind of own controls, without waiting for the arrival of HTML5, HTML6 or HTML8.

Now consider the directive, which has no analogues in HTML
 <ANY ng-repeat="book in books"> 

On the face of the use of logic in the tags. In fact this is not true. The basic logic, which is much more complicated, is packaged in a script that describes the directive. Only the interaction interface is in the attribute. The developers of Angulyar believe (and it’s difficult to disagree with them) that the markup should speak not only about WHAT elements are located on the page, but also about WHAT they do. A description of how they do it - the lot of the script.

Of course, the directive can be crammed and complex logic. In general, almost any behavior can be organized from a set of standard directives. Sometimes this is justified, especially when you need to do something quickly, but Angulyar does not promote this approach 100%.

Everyone already knows that directives can be written in several ways.
 <my-dir></my-dir> <span my-dir="exp"></span> <span class="my-dir: exp;"></span> <!-- directive: my-dir exp --> 

So, the most preferred (close to ideology) first method. In some cases (for example, for directives like ng-repeat, or for compatibility, including psychological) - the second. The third is made just like that. The fourth is needed to bypass HTML restrictions, for example, to put something other than <td> in the <tr> . Also, for better compatibility with older browsers, it is recommended to start the directive name with a prefix (for example, my-). For validators, you can add prefixes x- or data- to names (they will be rejected by Angular).

Flash control


In other words, how to make the page load smoother for the user.

First, place the Angulyar scripts at the end of the page so that the user does not look at the white screen while they are loaded.

Secondly, pay attention to the ng-cloak directive, which hides the pattern until it is fully processed by Angulyar, and also use ng-bind instead of the expression in curly brackets {{ }} .

In fact, in most simple cases, flicker problems simply do not arise.

Difference of controllers from services


Controllers describe the behavior of a view, i.e. answer the questions what will happen if you click on the X button and where to work with the X data . For each type of application, a separate controller instance is created. In no case should the controllers be manipulated with the DOM. Work with DOM is performed only in directives.

Services contain the basic logic and answer the question of what and how X does . Unlike controllers, services are singletones, that is, only one service instance can exist in the entire application. They also do not need to work with DOM, but sometimes you can, for example, if you need to make a global dialog box, etc. In any case, the use of DOM-manipulations in services must be limited.

Degradation of govnokod


From the foregoing, you can determine the strategy to combat govnokodom. During experiments, rummaging and so on. simple logic can be written directly in HTML, as it becomes more complex, it must be transferred to separate directives and controllers. Further, all non-representational logic is submitted to services. It turns out a kind of local refactoring.

Scope


The field of view links the view with the controller. It is intended only for reading in templates (form) and only for writing in controllers .

Do not confuse the scope with the model. The scope is a link to the model . Therefore, it is wrong to write model parameters into the scope directly. It is necessary to create one parameter and place the model there.
 // $scope.param1 = 'hello'; $scope.param2 = 'world'; // $scope.model = { param1: 'hello', param2: 'world' }; 

The difference in these approaches is clearly demonstrated in this video , as well as in the first example (see comments in the code).

Accordingly, the appeal to the model in directives will most often look like this: : , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .
: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .
: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }} ). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .
: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }}
). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }}
). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

: , - .


, .. , . . . .

, , , ( ). , .



- GZIP- ( ) index.html, .. , , . ( ): , , , CSS.
( ), . .

, :
, , ( , {{ }}
). .
$watch {{ }} . , , , .

ng-view ng-include DOM- , , ng-show ng-hide . ng-repeat , . ., , . , . . , , , . , . ng-repeat . .

DOM ng-include innerHTML , . . .


, - , , . API , .

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


All Articles