Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require:ngModel vs. scope: { ngModel: '=' } on AngularJS Directives

Hi which is better? What is the difference? What are the pros and cons?

Here is the comparision code between the two:

scope: { ngModel:'=' }

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="app">

<input ng-model="code"></my-directive>

</div>
 <script type="text/javascript">
  app = angular.module('app', []);

  app.directive('input', function(){
   return {
    scope: {
     ngModel: '='
    },
    link: function(scope, element, attrs){
     scope.$watch('ngModel', function(value){
      console.log(value);
     })
    }
   }
  });
 </script>
</body>
</html>

require: 'ngModel',

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="app">

<input ng-model="code"></my-directive>

</div>
 <script type="text/javascript">
  app = angular.module('app', []);

  app.directive('input', function(){
   return {
    require: 'ngModel',
    link: function(scope, element, attrs, ngModel){
      attrs.$observe('ngModel', function(value){
        scope.$watch(value, function(newValue){
          console.log(newValue);
        });
      });
    }
   }
  });
 </script>
</body>
</html>

PS Please be aware that both codes does the same. Logs on the console with the value of the model

like image 449
Dean Christian Armada Avatar asked Jun 03 '16 11:06

Dean Christian Armada


People also ask

What is the difference between the NG bind and NG-model directives?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.

What does the [( ngModel )] directive do in angular?

The ngModel directive is a directive that is used to bind the values of the HTML controls (input, select, and textarea) or any custom form controls, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during form validations.

Does Ng change require NG-model?

The ng-change directive requires a ng-model directive to be present. The ng-change directive from AngularJS will not override the element's original onchange event, both the ng-change expression and the original onchange event will be executed.

What is ngModel and Ngvalue?

ng-value="'hard'" ng-model is intended to be put inside of form elements and has two-way data binding ($scope --> view and view --> $scope) e.g. <input ng-model="val"/>. or you can say The ng-model directive binds the value of HTML controls (input, select, textarea) to application data.


1 Answers

With scope:{ngModel:'='} ,

It creates a directive with isolate scope , here basically scopes are isolated and its doesn't inherit from the parent $scope , but values are passed into the directive which are required for this directive. if your use '=' then its two way data binding .

well advantages and disadvantages depends on your requirement .

Advantages :

  • no need to use $scope.$watch every time , in order to update the view in your directive if values of the parent scope variables changes. '=' does the job .
  • If directive is used with isolate scope , it will act as reusable component ,can use in multiple places in your application.
  • scope variables passed to isolated scopes can be used in your directive controller directly even if the link function doesn't exist in your directive.

disadvantages :

  • As scopes are isolated , will not get entire scope variables/functions of the parent controller . needs to passed via the directive definition only
  • cant able to use angular built-in methods for ng-model or ng-form to create api , ngFormController , ngModelController

with require:'ngModel'

Advantages:

  • easy to access entire parentsController scopes/functions when its used in nested directives
  • good to create plugin as helps in modularity and as it has parent child relationship
  • able to use angular built-in methods for ng-model or ng-form to create api out of them
  • works well with events emitting and broadcasting (publish-subscribe design pattern).

disadvantage

  • Should have link function in-order to get the parents controller and its scopes variables and methods.
  • need to use $scope.$watch to update the view if parents scope variables changes.
  • when controller As syntax is used . need to have $scope in-built methods like $scope.$watch and $scope.$on $scope.$emit , as it will be disturbance in directives controller or link with respect to use of both this and $scope
like image 94
Shushanth Pallegar Avatar answered Oct 15 '22 13:10

Shushanth Pallegar