George Mauer George Mauer - 19 days ago 5
AngularJS Question

How to access component controller when testing?

I am trying to put together some tests of a component. I have the following

describe(`selecting an item`, () => {
let element, $scope;

beforeEach(module('my-app'))
beforeEach(inject(($compile, $rootScope) => {
$scope = $rootScope;
element = $compile(`
<item-selector parent-id="pid">
</item-selector>
`)($scope)

//call $ctrl.activate() on the component's controller
}))

})


the controller for
item-selector
has an
activate()
method. How can I invoke it from my test?
$scope
doesn't seem to contain any reference.

Answer
let ctrl = element.controller('item-selector')