doubt with angularjs modules and directives [closed]

0

I have the following code:

  

1.- Why use directives ?, in this case it would not be enough to use controller?

     

2.- Does the controller always have a function with scope?

(function() {

  var modulo=angular.module('tnt.ui.components', []);
  modulo.directive('userInfo', [function() {
    return {
      restrict: 'E',
      template:'Nombre: {{user.name}}, email: <a href="mailto:{{user.email}}">{{user.email}}</a>'
    };
  }]);
  
  
  
 
  modulo.controller('DemoDirectivesCtrl', function($scope){
      $scope.user = {
        name: 'Jose',
        email: '[email protected]'
      };
  });

}());
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body>
    
<div ng-app="tnt.ui.components" ng-controller="DemoDirectivesCtrl">
    <user-info />
</div>

  </body>

</html>
    
asked by hubman 27.04.2017 в 17:02
source

1 answer

1

With module you define an application, that is to say that all the magic that AngularJS can provide to your HTML will be covered by this module.

The directives can be understood as attributes that you can add to your HTML tags and that allow you to extend the functionality of the code block as snippets .

With controller you add all the logic to all the elements that you define in the rest of your application.

The scope variable serves as a glue between your view and your controller.

I recommend you visit the W3Schools and see in more detail each of the points you have doubts about

    
answered by 27.04.2017 в 19:00