samedi 2 mai 2015

How to directive in link phase?

Here is some Javascript that makes a call to jqLite's trigger inside the link function of an AngularJS directive.

angular.
  module('myApp').
  directive('myDirective',
    function($timeout) {
      return {
        link: function(scope, element) {

          scope.resetChosenElements = function() {
            $timeout(function() { 
                element.find('[chosen]').trigger('chosen:updated'); 
            }, 0);
          }

          scope.resetChosenElements();          

        }
      };
    }
  );

How can I write a test that checks that trigger is called when the directive is created without spying on $.trigger? (I don't want to spy on $.trigger because that catches all calls made to it, including those form other directives).

Is there a way to spy on the element argument that can be passed to link?

Aucun commentaire:

Enregistrer un commentaire