title: AngularJS: How to properly set/unset a variable with a directive repeated using ng-repeat?
body: <p>I wrote a directive that essentially should set a variable of the global scope to a certain value, if the mouse is clicked on it and return to the default value, once the mouse is released.</p>
  
  <p>I got it to work but as I am just starting with angular I really try to do things proper and avoid even starting with bad programming habits.</p>
  
  <p>There's also some DOM manipulation happening in the directive but I simplified it to the basic functionality of changing the value.</p>
  
  <p>This is the HTML:</p>
  
  <pre><code>&lt;th ng-repeat="item in list" my-directive="{{$index}}" ng-model="clicked.current"&gt;&lt;/th&gt;
  </code></pre>
  
  <p>And this is the (simplified) directive:</p>
  
  <pre><code>app.directive("myDirective", function () {
      return {
        scope: {
          model: "=ngModel",
          index: "@myDirective"
        },
        template: "&lt;button ng-mousedown='mousedown()'&gt;click&lt;/button&gt;",
        link: function(scope, elm, attrs) {
          scope.mousedown = function () {
            scope.model = scope.index;
            angular.element(document).one("mouseup", function () { // react even if dragged out of element bounds
              scope.$apply(function () {
                scope.model = -1;
              });
            });
          };
        }
      };
    });
  </code></pre>
  
  <p>It is working as expected but I am unsure if it is the best and most correct way of achieving this.</p>
  
  <p>My Questions are:</p>
  
  <ol>
  <li>For this purpose - did I chose the correct approach?</li>
  <li>Is it okay, that I defined the mousedown method in the link function or should I define a seperate controler for the directive using the "controller" option?</li>
  <li>Is there a way to achieve the same thing (i.e. with just one parameter to the directive)? (I read one should avoid using scope.$parent.$index).</li>
  <li>General comments / advice?</li>
  </ol>
  
  <p>Thank you for your time!</p>