如何在指令的单元测试中检查是否已调用$ emit?
我的指令相当简单(为了说明的目的):
angular.module('plunker', [])
.directive('uiList', [function () {
return {
scope: {
lengthModel: '=uiList',
},
link: function (scope, elm, attrs) {
scope.$watch('lengthModel', function (newVal) {
scope.$emit('yolo');
});
}
}
}])
Run Code Online (Sandbox Code Playgroud)
所以每次属性uiList改变时,它都会发出事件.
我的单元测试代码如下:
describe('Testing $emit in directive', function() {
var scope;
var element;
//you need to indicate your module in a test
beforeEach(function () {
module('plunker');
inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
scope.row= 1;
spyOn(scope,'$emit');
element = angular.element('<ul id="rows" ui-list="row">');
$compile(element)(scope);
});
});
it('should emit', function() {
scope.$digest();
scope.row …Run Code Online (Sandbox Code Playgroud) unit-testing angularjs angularjs-scope karma-runner karma-jasmine
我看到有些人在他们的代码中写了这个布尔值相等,我通常把常量放在"=="运算符的右边.我注意到0 == a比a = 0更快的操作.有人可以解释为什么吗?它的最佳实践是什么?
我有$(document)和$(window),它们分别与'ready'和'resize'事件绑定.他们共享相同的事件处理程序.
码:
$(window).on('resize', function () {
Shared code
});
$(document).ready(function () {
Shared code
});
Run Code Online (Sandbox Code Playgroud)
而不是上面的风格,有一种传统的处理方式,以使代码清洁和简单>
Angular是否有内置指令来舍入输入值?
在这种情况下,数字过滤器是不合适的,因为我也希望对实际val的ng模型进行舍入.如果我要写一个,它会是什么样的?
我刚刚开始学习angularJs,并尝试使用角度路由服务配置部分页面.
它使用哈希格式,但是,当我试图摆脱哈希时,routeProvider停止工作.
JS
app.config(function($routeProvider,$locationProvider){
$locationProvider.html5Mode(true);
$routeProvider.when("/", { templateUrl: "/index.html" }).
when("/about", { templateUrl: "/partials/about.html" }).
when("/contact", { templateUrl: "/partials/contact.html" }).
otherwise({ redirectTo: '/' });
});
Run Code Online (Sandbox Code Playgroud)
HTML
<ul class="nav navbar-nav navbar-right">
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul
Run Code Online (Sandbox Code Playgroud)
有人可以开导我吗?