由 Yeoman 使用 angular-generator 生成的应用程序。
指示:
angular.module('psApp').directive('scrollTop', function () {
return {
restrict: 'A',
scope: true,
template: '<a href="#" ng-click="click()" class="scroll-top"><span class="glyphicon glyphicon-arrow-up"></span> Back to top</a>',
controller: ['$scope', '$element', '$document', function ($scope, $element, $document) {
$scope.click = function () {
$document.scrollTop(0, 500);
};
}]
};
});
测试:
describe('Directive: scrollTop', function () {
// load the directive's module
beforeEach(module('psApp'));
var scope, element;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
element = $compile('<div scroll-top></div>')(scope);
scope.$apply();
}));
it('should have "Back to top" as text', inject(function () {
expect(element.html()).toBe('<a href="#" ng-click="click()" class="scroll-top"><span class="glyphicon glyphicon-arrow-up"></span> Back to top</a>');
expect(element.text()).toBe(' Back to top');
element.click();
}));
});
错误:
PhantomJS 1.9.7 (Mac OS X) 指令:scrollTop 应该有“Back to top”作为文本类型错误:'undefined' 不是函数(评估'element.click()')
我无法理解问题出在哪里。:( 请发布功能代码。