我有一个像这样的AngularJS指令示例< div some-dir =“5”/>
如何在我的测试中访问此指令属性值5?
describe("some-dir",function() { var element,scope; beforeEach(module('app')); beforeEach(inject(function($rootScope,$compile) { scope = $rootScope; element = angular.element('<div><div id="el1" some-dir="5" /></div>'); $compile(element)(scope); scope.$digest(); })); it('should be able to get the attribute value',function(){ // get the attr value of some-dir }); });
您可以使用其isolateScope方法检查元素的范围值.但是,当您在指令属性旁边传递值时,这将不起作用,因为这些值不会复制到隔离范围中.
在这种情况下,可以使用element.attributes方法获取并测试该值.
首先编译你的指令html:
var element; beforeEach(inject(function (_$compile_,_$rootScope_) { var $compile = _$compile_,$scope = _$rootScope_; element = $compile('<div my-directive="4" some-value="5"></div>')($scope); $scope.$digest(); }));
然后你可以期望element的isolateScope返回一个带有someValue属性的对象.
it('should expect some-value as 5',function () { inject(function ($injector) { // check attribute values using isolateScope expect(element.isolateScope().someValue).toEqual(5); // check the value right after directive attribute expect(element.attr('my-directive')).toEqual('4'); }); });
原文地址:https://www.jb51.cc/angularjs/141962.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。