1

我有一个指令在单元测试中呈现以下 html:

<div id="published-envs">
    <strong>Published to</strong>
    <!-- ngRepeat: env in fundConfigurationVersion.publicationEnvironments --><div data-ng-repeat="env in fundConfigurationVersion.publicationEnvironments" class="ng-binding ng-scope">
            QA (Jan 29, 2015)
    </div><!-- end ngRepeat: env in publicationEnvironments --><div data-ng-repeat="env in publicationEnvironments" class="ng-binding ng-scope">
            PROD (Jan 30, 2015)
    </div><!-- end ngRepeat: env in publicationEnvironments -->
</div>

我试图抓住<div id="published-envs">并断言它有两个 child <div>

我尝试了以下方法,但它不起作用: var environments = element.find('#published-envs'); //comes back empty

在角度单元测试中按 id 查找元素的正确方法是什么?

4

1 回答 1

1

根据Angular Element Docs.find()

仅限于按标签名称查找

尝试

var environments = angular.element(document.getElementById('published-envs')).find('div');

JSFiddle 示例

编辑

对于编译后的标记,本质上是一样的,只是注入$compile服务并使用如下

var comipledEnvironments = $compile(angular.element('<div id="published-envs"><div></div><div></div></div>'))($scope).find('div')

console.log(comipledEnvironments); // 2 <div>'s

更新了 JSFiddle

于 2015-02-02T22:56:27.497 回答