Here's another suggestion that supports your logic in much the same way: use the second directive to make the target element available on the controller, which can then be passed to the main directive for processing: http://jsfiddle.net/morloch/p8r2Lz1L/
getElement
.directive('getElement', function() { return { restrict: 'A', scope: { getElement: '=' }, link: function postLink(scope, element, attrs) { scope.getElement = element; } }; })
myDirective
.directive('myDirective', function() { function MyController() { var vm = this; vm.onSelection = function() { vm.targetElement.val('calculated stuff'); vm.targetElement.trigger('input'); } } return { template: '<div></div>', restrict: 'E', scope: { targetElement: '=' }, controller: MyController, controllerAs: 'vm', bindToController: true }; })
Test
describe('Directive: myDirective', function() { // load the directive module beforeEach(module('testApp')); var element, controller, scope; beforeEach(inject(function($rootScope, $compile) { scope = $rootScope.$new(); element = angular.element('<input get-element="elementBar" type="text" id="bar"><my-directive target-element="elementBar"></my-directive>'); $compile(element)(scope); scope.$digest(); controller = $(element[1]).controller('myDirective'); })); it('should have an empty val', inject(function() { expect($(element[0]).val()).toBe(''); })); it('should have a calculated val after select', inject(function() { controller.onSelection(); expect($(element[0]).val()).toBe('calculated stuff'); })); });
source share