1

I was wondering about the difference between scope and model within a directive.

The following 2 directives behave exactly the same:

angular.module( 'exampleApp', [] )
.directive( 'exampleDirective1', function() {
  return {
    restrict: 'E',
    scope: { // using scope here
      info: '='
    },
    template: '<div><span>{{ info }}<span> <input ng-model="info"></div>'
  };
} )
.directive( 'exampleDirective2', function() {
  return {
    restrict: 'E',
    model: { // using model here
      info: '='
    },
    template: '<div><span>{{ info }}<span> <input ng-model="info"></div>'
  };
} );

Am I missing something?

lumio
  • 7,428
  • 4
  • 40
  • 56
  • 2
    scope defines an isolate scope, and allows defining info by passing an expression as the info attribute of the directive. model is just a random field of your directive object, that angular doesn't care about. Those two directives don't behave the same way at all. Use ``, and you'll see. http://plnkr.co/edit/SuCzIFYwQ85lAIUufPz6?p=preview – JB Nizet Jul 06 '16 at 22:05
  • Nice! I only tested my code with a given variable from a parent directive. Thanks for your answer! – lumio Jul 07 '16 at 15:30

0 Answers0