我已经使用自定义指令来检测角度2中元素外部的单击,但角度4中不可能相同.
[plunkr] https://plnkr.co/edit/aKcZVQ?p=info
当我尝试在angular-4中使用相同的代码时,我收到以下错误:
1. Argument of type '{ template: string; directives: typeof ClickOutside[]; }' is not assignable to parameter of type 'Component'. ==>
@Component({
templateUrl: "",
directives: [ClickOutside]
})
2. Type 'Subscription' is not assignable to type 'Observable<MouseEvent>'. in the directive inside ngOnInit() and ngOnDestroy()
ngOnInit() {
this.globalClick = Observable
.fromEvent(document, 'click')
.delay(1)
.do(() => {
this.listening = true;
}).subscribe((event:MouseEvent) => {
this.onGlobalClick(event);
});
}
ngOnDestroy() {
this.globalClick.unsubscribe();
}
Run Code Online (Sandbox Code Playgroud)
如果角度4中的指令声明有任何变化请告诉我,官方文档对此事没有任何帮助.
angular-directive angular2-directives angular angular-observable
我在Angular 4应用中有类似的东西(出于示例的原因,我删除了代码)
@Injectable()
export class SomeService {
constructor(
private http: Http
) {
}
get(id: number) {
return this.http.get('http://somedomain/somemodel/${id}.json');
}
}
Run Code Online (Sandbox Code Playgroud)
一些组件使用它来进行API调用。
constructor(private someService: SomeService) {}
...
someMethod() {
// code here...
this.someService.get(2).subscribe( someHandlerFunction );
}
someOtherMethod() {
// more code here...
this.someService.get(2).subscribe( someHandlerFunction );
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道何时调用someMethod()和someOtherMethod()。有时可能会同时调用它们,然后,我的API将被调用两次。我要查找的是是否有任何方法可以更改我的服务以仅在X数量的时间后执行此请求。我尝试使用去抖动:
get(id: number) {
return this.http.get(`http://somedomain/somemodel/${id}.json`).debounceTime(10000);
}
Run Code Online (Sandbox Code Playgroud)
期望(在这种情况下)此HTTP get请求将仅在10秒后重复,如果在10秒内发出请求,则该请求将不会重复,但是observable会发出最后一个值。但这没有用。有小费吗?
PS:我知道我可以使用某种标志来控制它,但是只要它不能很好地缩放,就不能这样做。我有许多带有许多HTTP请求的服务。
有什么想法吗?
我想对我正在使用的函数进行基准测试,以确定对象数组中是否存在重复属性,使用map()和some()对比另一个执行相同操作但使用for()另一个内部函数的函数for().
let array = [ { "value": 41}, { "value": 12}, { "value": 32} ];
let itens = array.map(x => x.value);
let haveDuplicate = itens.some((item, idx) => itens.indexOf(item) !== idx);
Run Code Online (Sandbox Code Playgroud)
与:
let array = [ { "value": 41}, { "value": 12}, { "value": 32} ];
let haveDuplicate = false;
for (let i = 0; i < array.length; i++) {
let x = array[i];
for (let j = (i + 1); j < …Run Code Online (Sandbox Code Playgroud) 我得到了 2 个具有相同 HTML 的表,并且都声明为:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-12 col-md-6">
<div class="card">
<div class="card-body">
<table class="table table-sm table-hover table-responsive">
<thead>
<tr>
<th>Item 01</th>
<th>Item 02</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 01</td>
<td>Item 02</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
这就是我在屏幕上看到的:
第二和连续的表使用相同的HTML声明没有得到100%的宽度thead和tbody...这里有什么不对?
更新:
由于最后一行的内容,我的第一个表正在工作,它足够长以使其 100% 适合我的卡片 div。
实际上 这是 Bootstrap 4 上的一个问题,所以这个问题上的 -2 声誉是无效的。谢谢。
angular ×2
javascript ×2
bootstrap-4 ×1
ecmascript-6 ×1
html ×1
observable ×1
rxjs ×1
typescript ×1