gpt4 book ai didi

javascript - 在 jasmin 中使用测试 Angular 复选框

转载 作者:数据小太阳 更新时间:2023-10-29 05:10:18 27 4
gpt4 key购买 nike

我的情况如下:

指令

scope: { foo:'=' },
template: '<input type="checkbox" ng-model="foo"/>'

父 Controller

$scope.foo = false;

Jasmine 测试

var cb = iElement.find('input');

$timeout(function() { // using $timeout to ensure $digest() isn't the issue.
cb.prop('checked',!cb.prop('checked'))
},0);

expect(cb.prop('checked')).toBe(true); // passes

expect($scope.foo).toBe(true); // doesn't pass

我的问题:为什么当我发出 prop('checked') 时 $scope.foo 没有得到更新,即使 DOM 更新了(正如我在检查后验证的那样)。

这是一个粗略演示问题的jsbin http://jsbin.com/kapifezi/2/edit

最佳答案

所以完整的答案是:您需要更改'checked'属性,然后触发点击事件。

var input = element.find('input');
input.prop('checked',!input.prop('checked'));
// input.triggerHandler('click');
input.triggerHandler('change');

谢谢凯文,这对我帮助很大!

关于javascript - 在 jasmin 中使用测试 Angular 复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22774995/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com