I'm currently using directiveElement.data("$ngModelController")
to get access to the element's $ngModelController
, as in the following example.
describe("directiveElement", function () {
it("should do something with ngModelController", inject(function($compile, $rootScope) {
var directiveElement = $compile("<input ng-model="myNgModel" customDirective type="text"></input>")($rootScope);
$rootScope.$digest();
var ngModelCtrl = directiveElement.data("$ngModelController");
ngModelCtrl.$modelValue = "12345";
// do rest of test
}));
});
However, I want to know if there is a better to access the $ngModelController
, or if accessing the $ngModelController
is a bad idea?
You could also do directiveElement.controller('ngModel')
.
I certainly think there are legitimate testing reasons why you would want a handle on this, though the more common way is to get a handle on it through the form (eg. https://github.com/angular/angular.js/blob/master/test/ng/directive/formSpec.js)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With