How should I access an element's angularjs $ngModelController in a jasmine unit test?

二次信任 提交于 2019-12-06 22:48:39

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/15836908/how-should-i-access-an-elements-angularjs-ngmodelcontroller-in-a-jasmine-unit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!