我应该如何在茉莉花单元测试中访问元素的angularjs $ ngModelController? [英] How should I access an element's angularjs $ngModelController in a jasmine unit test?

查看:117
本文介绍了我应该如何在茉莉花单元测试中访问元素的angularjs $ ngModelController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 directiveElement.data($ ngModelController)来访问元素的 $ ngModelController ,如下例所示。

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
  }));
});

但是,我想知道是否有更好的访问 $ ngModelController ,或者如果访问 $ ngModelController 是个坏主意?

However, I want to know if there is a better to access the $ngModelController, or if accessing the $ngModelController is a bad idea?

推荐答案

你也可以做 directiveElement.controller('ngModel')

我当然认为有合理的测试理由可以解释为什么要对此进行处理,不过更常见的方法是通过表单处理它(例如。 https://github.com/angular/angular.js/blob/ master / test / ng / directive / formSpec.js

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)

这篇关于我应该如何在茉莉花单元测试中访问元素的angularjs $ ngModelController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆