title: Creating a autosave directive in angularjs
body: <p>I want to create a directive as an attribute where i want to attach an autosave capability to other directives. I thought about creating a directive which puts a watch on an attribute (model) passed in. The debounce function is from lodash and prevents an execution on each keystroke. The put method is from the restangular model.</p>
  
  <p>My idea so far</p>
  
  <pre><code>angular.module('myApp.directives', []).directive('autosave',[ function () {
      'use strict';
  
      function autosaveController($scope) {
  
          function saveModel(newModel, oldModel) {
              if(newModel !== oldModel &amp;&amp; newModel.put) {
                  $scope.model.put();
              }
          }
  
          $scope.$watch($scope.model, _.debounce(saveModel, 5000), true);
      }
  
  return {
  
      restrict : 'A',
  
      controller: ['$scope', function($scope) {
          return autosaveController($scope);
      }],
  
      scope : {
          model : '='
      }
  
  };
  
  }]);
  </code></pre>
  
  <p>But i did not get this to work, then i thought may i better put this in my restangular model, but i havent got scope there. How do i add this autosave feature properly? I need to be able to define which object in the scope will be watched and attach a watch to it so i get updates on changes.</p>
  
  <p>Edit: Created a plunker showing the idea:</p>
  
  <p><a href="http://plnkr.co/edit/I8GK8zHV8fOXY0zMa1AL?p=preview" rel="nofollow">http://plnkr.co/edit/I8GK8zHV8fOXY0zMa1AL?p=preview</a></p>
  
  <p>I tried to set a scope on both directives and got an error that this is not allowed. (something like two isolated scopes not allowed)</p>
  
  <p>Can i create an isolated scope on a attribute directive at all?</p>
  
  <p>Basically i need to pass in the model to the autosave directive</p>