我正试图窥探指令的$ emit,但不知怎的,我不能让间谍'听'$ emit.
这是我的指令控制器中的代码:
$scope.$on('send', function () {
console.log('called');
$scope.$emit('resultSend', {'ok': true, 'data': ''});
});
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试:
var $rootScope, $compile, elm, element;
beforeEach(inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$compile = $injector.get('$compile');
elm = angular.element('<test></test>');
element = $compile(elm)($rootScope);
}));
it('should listen for the send broadcast and emit the resultSend', function () {
spyOn($rootScope, '$emit');
$rootScope.$broadcast('send');
expect($rootScope.$emit).toHaveBeenCalledWith('resultSend');
});
Run Code Online (Sandbox Code Playgroud)
console.log输出('called')由Karma打印出来,所以我猜单元测试广播事件确实有效.
这是否与$ emit相关而不是向下播放,如果是这样,我该如何捕获它,如果不是,我该如何处理这种情况呢?
假设以下简单测试:
@Test
public void test() throws Exception {
Object value = 1;
assertThat(value, greaterThan(0));
}
Run Code Online (Sandbox Code Playgroud)
测试不会编译,因为"greaterThan"只能应用于类型的实例Comparable
.但我想断言这value
是一个大于零的整数.我怎样才能用Hamcrest来表达呢?
简单的解决方案是通过强制转换匹配器来删除泛型:
assertThat(value, (Matcher)greaterThan(0));
Run Code Online (Sandbox Code Playgroud)
可能,但生成编译器警告并感觉不对.
一个冗长的选择是:
@Test
public void testName() throws Exception {
Object value = 1;
assertThat(value, instanceOfAnd(Integer.class, greaterThan(0)));
}
private static<T> Matcher<Object> instanceOfAnd(final Class<T> clazz, final Matcher<? extends T> submatcher) {
return new BaseMatcher<Object>() {
@Override
public boolean matches(final Object item) {
return clazz.isInstance(item) && submatcher.matches(clazz.cast(item));
}
@Override
public void describeTo(final Description description) {
description
.appendText("is instanceof …
Run Code Online (Sandbox Code Playgroud) I have got the following test:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Component } from '@angular/core';
@Component({
template: '<ul><li *ngFor="let state of values | async">{{state}}</li></ul>'
})
export class TestComponent {
values: Promise<string[]>;
}
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let element: HTMLElement;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [TestComponent]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
element = (<HTMLElement>fixture.nativeElement);
});
it('this test fails', async() => …
Run Code Online (Sandbox Code Playgroud) 在基于Spring 3的Web(portlet)应用程序中,我有一个控制器,其方法如下:
@RenderMapping
public ModelAndView handleRenderRequest(...,@RequestParam MyClass myObject)
{
...
}
Run Code Online (Sandbox Code Playgroud)
现在我想知道:我如何告诉spring如何将请求参数转换为MyClass.我找到了有关属性编辑器和Converter接口的信息,似乎有一些影响,转换器是属性编辑器的继承者,但似乎没有人喜欢明确它.
我实现了String到MyClass转换的转换器接口.但是我如何告诉Spring呢?我尽可能使用基于注释的配置,所以我检查spring是否会自动从类路径中检测到Converter,但事实并非如此.
因此,从手册中配置ConversionService的部分想告诉我,我必须将以下内容添加到我执行的applicationContext.xml中:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="some.package.MyConverter"/>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
还是有点:
org.springframework.beans.ConversionNotSupportedException:无法转换值[...]
那我错过了什么?有没有办法,只需配置一个包,让Spring扫描这个包转换器并自动注册?并且说,在某种方法中,我想使用与所有其他方法不同的转换器.例如,我想要一个检查Luhn-Checksum的整数并删除校验和,我该怎么做?像@RequestParam(converter = some.package.MyConverter.class)之类的东西会很棒.
好的,我刚收到文档:
当您在客户端环境(例如Web应用程序)中工作时,请使用Formatter SPI,并且需要解析和打印本地化的字段值
所以我想这意味着我应该使用Formatter SPI,这是属性编辑器和转换器旁边的第三种可能性(我认为我可以使用比较表等).我也实现了Parser接口,并尝试使用以下命令注册我的转换器:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.SortOrderEnumConverterSpring"/>
</set>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用"set"而不是"list"来指定转换器.我在FormattingConversionServiceFactoryBean.setConverters方法中设置了一个调试断点,该方法在使用list时没有触发,但它确实触发了使用set.
另外我补充道
<mvc:annotation-driven conversion-service="conversionService"/>
Run Code Online (Sandbox Code Playgroud)
以及我的applicationContext的mvc-prefix的命名空间.但我仍然得到转换不支持的异常.
我还尝试回到转换器方法,并在我的applicationContext.xml文件中更改了从列表到设置的转换器的参数列表,但这也没有改变任何东西.
正如digitaljoel指出的那样,可以使用initBinder方法为每个控制器设置不同的转换器.我将它应用于我的控制器:
@Autowired
private ConversionService conversionService;
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setConversionService(conversionService);
}
Run Code Online (Sandbox Code Playgroud)
在我的applicationContext.xml中:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.with.MyConverter"/>
</set>
</property> …
Run Code Online (Sandbox Code Playgroud) 我有一个angularjs模板,看起来类似于:
<img ng:src="/resources/{{id}}/thumbnail" />
Run Code Online (Sandbox Code Playgroud)
但是这会导致$ interpolate:noconcat错误.与此模板相反:
<img ng:src="{{fullUrl}}" />
Run Code Online (Sandbox Code Playgroud)
甚至:
<img ng:src="{{id|createThumbnailURL}}" />
Run Code Online (Sandbox Code Playgroud)
(其中createThumbnailURL是一个简单的过滤器,它执行与上面相同的连接)工作完全正常.
文件说:
连接表达式使得很难推断连接值的某些组合是否使用不安全并且很容易导致XSS.
是的,静态URL总是比连接的更容易评估,我在那里看到了重点.然而,拥有可以通过简单连接构建的URL的REST-API对于我来说并不常见,并且必须完成连接.我可以在控制器甚至服务器端执行此操作,但是如何改进任何内容以将串联移动到其他位置?而什么是解决这个问题的推荐的方法?
UPDATE
这是错误的演示:http://cipher-code.de/tmp/angular3/index.xhtml
也许它与页面是XML有关.
我有一个元素数组,用户不仅可以编辑,还可以添加和删除完整的数组元素。除非我尝试在数组的开头添加一个值(例如使用unshift
),否则此方法效果很好。
这是证明我的问题的测试:
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
@Component({
template: `
<form>
<div *ngFor="let item of values; let index = index">
<input [name]="'elem' + index" [(ngModel)]="item.value">
</div>
</form>`
})
class TestComponent {
values: {value: string}[] = [{value: 'a'}, {value: 'b'}];
}
fdescribe('ngFor/Model', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let element: HTMLDivElement;
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [FormsModule],
declarations: [TestComponent]
});
fixture …
Run Code Online (Sandbox Code Playgroud) 我有一个angularJs应用程序,我用grunt构建,服务器后端用Java编写,运行在tomcat服务器上.在开发时我想要使用grunt-connect-proxy连接它们.但我甚至无法让它工作.
我在网上找到的所有"示例"和"演示"都使用了几百行Gruntfile.js.结果证明找不到问题并不是很有用.什么是一个最小的(!),例如什么样子的?
我有一个蚂蚁脚本,它有一个taskdef
和任务创建一个https互联网连接,并且有一些SSL的东西是错误的.因此,我想设置系统属性javax.net.debug=all
以获取更多信息.
在java中我会使用该-D
选项执行此操作,但在ant中,这用于与系统属性不同的ant属性.
如果这不是一个taskdef
而是一个java
任务,我可以使用该sysproperty
属性,但它不是 - java
任务.
谷歌搜索这是令人沮丧的复杂,因为蚂蚁的蚂蚁属性和系统属性非常相似,大多数搜索结果都是关于另一个(或关于java
-task).
在我的apache配置中,我有一个像这样配置的虚拟主机:
Alias /mediamanager /storage/files/mediamanager
<Directory /storage/files/mediamanager>
DirectoryIndex /mediaManagerIndex.php
DAV On
# ... And some authentication directives ... #
</Directory>
Run Code Online (Sandbox Code Playgroud)
这个想法是有人可以通过WebDAV-Client和简单的Web浏览器访问文件,在这种情况下,PHP脚本会生成一些漂亮的目录视图.
这在Apache 2.2中运行得很好,但最近我升级到了Apache 2.4,现在它已经坏了.我非常怀疑我患有这个已经2岁并且没有修复的虫子.建议的解决方法添加:
<Limit PROPFIND>
DirectoryIndex never-encounterable-file-name.html
</Limit>
Run Code Online (Sandbox Code Playgroud)
对我不起作用.可能是因为我仍然想要一个目录索引.如果我DirectoryIndex
完全删除我的WebDAV再次工作(此目录中不存在index.html或类似的文件),但当然我放弃了使用我的PHP文件作为目录索引的能力.我尝试在a中指定我的DirectoryIndex,<Limit GET>
但这没有任何效果.
有没有办法让Debian和DirectoryIndex在Debian上的Apache 2.4中同时工作(如果可能,无需更改源代码和重新编译)?
我想设置<ng-content>
动态实例化组件的主体ComponentFactoryResolver
.
我看到我可以使用输入和输出访问ComponentRef
,但不能设置<ng-content>
.
请注意<ng-content>
我计划设置可以包含简单文本/可以跨越动态创建的组件
@Component({
selector: 'app-component-to-project',
template: `<ng-content></ng-content>`
})
export class ComponentToProject implements AfterContentInit {
ngAfterContentInit() {
// We will do something important with content here
}
}
@Directive({
selector: 'appProjectionMarker'
})
export class ProjectionMarkerDirective implements OnInit {
constructor(private viewContainerRef: ViewContainerRef, private componentFactoryResolver: ComponentFactoryResolver) {
}
ngOnInit() {
const componentFactory: ComponentFactory<ComponentToProject> = this.componentFactoryResolver.resolveComponentFactory(ComponentToProject);
const componentRef: ComponentRef<ComponentToProject> = this.viewContainerRef.createComponent(componentFactory);
// Question: How to set content before the child's afterContentInit is invoked …
Run Code Online (Sandbox Code Playgroud) angular ×3
angularjs ×2
javascript ×2
angular-test ×1
ant ×1
apache ×1
gruntjs ×1
hamcrest ×1
java ×1
proxy ×1
spring ×1
spring-mvc ×1
testbed ×1
unit-testing ×1
webdav ×1