有没有相当于$scope.emit()
或$scope.broadcast()
在角?
我知道EventEmitter
功能,但据我所知,它只会向父HTML元素发出一个事件.
如果我需要在fx之间进行通信怎么办?兄弟姐妹或DOM根目录中的一个组件和一个嵌套了几层深层的元素?
我正在编写一个组件,我需要访问本<audio controls>
机元素.我现在正在做它像这样通过获取它ngOnInit()
通过使用ElementRef
这样的this.elementRef.nativeElement.querySelector("audio");
虽然它有效,但我认为它非常不优雅,Angular2也警告使用ElementRef时的风险.
真的没有更简单的方法吗?
我可以将它标记为局部变量,<audio controls #player>
并通过this.player
某种方式从控制器访问本机元素或类似的东西吗?
import {Component, OnInit, ElementRef, Input} from 'angular2/core';
@Component({
selector: 'audio-preview',
template: `
<audio controls>
<source [src]="src" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
`
})
export class AudioPreview implements OnInit {
@Input() src: string;
constructor(public elementRef: ElementRef) {}
ngOnInit() {
var audioElement = this.getAudioElement();
audioElement.setAttribute('src', this.src);
}
getAudioElement() : HTMLAudioElement {
return this.elementRef.nativeElement.querySelector("audio");
}
}
Run Code Online (Sandbox Code Playgroud) 我需要li
在Angular2的列表中为每个项重复几个元素.在角度1.x我使用ng-repeat-start
和ng-repeat-end
为此.我无法在Angular 2中找到正确的方法.有一些较旧的博客文章,但他们的建议不适用于最新的Angular2测试版.
<li>
应该为每个类别重复所有元素:(我通常会对属性进行处理*ngFor="#category of categories"
- 但我找不到放在哪里...
救命?
<ul class="dropdown-menu" role="menu">
<li class="dropdown-header">
{{ category.title }}
</li>
<li>
<a href="{{ '/music/' + tag.keyword }}" *ngFor="#tag of category.tags" [hidden]="tag.deleted === 1">{{ tag.tag_title_da }}</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Alle musikstykker</li>
<li><a href="/music/all">Alle musikstykker</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud) 嗨其他Elixir程序员.
我有大约2.500个音乐曲目的列表,我想按不同的参数排序,例如曲目的标题.
排序应该不区分大小写.
下面的代码有效,但需要大约100ms到130ms来对列表进行排序.有更快的方法吗?对我来说,一个参考是Node.js,它在使用时大约需要25msArray.prototype.sort
编辑:对不起,我实际上已经不合时宜了.排序发生在大约30ms.但是,我仍然希望你的意见:排序能否更快完成?
谢谢.
defmodule MusicServer.Tracks.SortTracks do
def sort_tracks(tracks, "title", "desc") do
Enum.sort(tracks, fn track1, track2 ->
first_char(track1["title"]) <= first_char(track2["title"])
end)
end
def first_char(string) do
string
|> String.at(0)
|> String.downcase()
end
end
Run Code Online (Sandbox Code Playgroud)
数据结构的一个例子:
[
%{
"artist" => "Rolling Stones",
"title" => "Start It Up",
"bpm" => 100,
"createdAt" => "2018-04-27T09:08:04.428Z",
"updatedAt" => "2018-07-14T14:28:17.771Z"
},
%{
"artist" => "Al Green",
"title" => "Let's Stay Together",
"bpm" => 123,
"createdAt" => "2018-04-27T09:08:04.428Z",
"updatedAt" => "2018-07-14T14:28:17.771Z"
},
...
]
Run Code Online (Sandbox Code Playgroud) 学习在 Android 中使用 BroadcastReceiver 类,我编写了一个小程序来接收电池充电状态并将其写入一个活动中的三个 TextView 字段。
但是,我已将 BroadcastReceiver 作为一个单独的类,以使其更简单并与活动分开。因此,我必须找到一种方法来告诉我的 Activity 类电池数据已更新,或者,这是我的解决方案,将 TextView 字段的引用从 Activity 传递到 BroadcastReceiver 类。
有谁知道是否可以从 BroadcastReceiver 中创建一个回调方法来启动一个函数,f.ex。更新文本视图();在活动中?
这是源代码 - 请注意有两个 java 文件:http : //pastebin.com/qjCTsSuH
问候,尼尔斯。
我正在访问Echo Nest API,这要求我重复相同的uri参数名称bucket
。但是,我无法在Guzzle 6中完成这项工作。我从2012年开始阅读类似的文章,但是这种方法不起作用。
我尝试将其手动添加到查询字符串中没有任何成功。
一个示例API调用可以是:
http://developer.echonest.com/api/v4/song/search?format=json&results=10&api_key=someKey&artist=Silbermond&title=Ja&bucket=id:spotify&bucket=tracks&bucket=audio_summary
这是我的示例客户端:
/**
* @param array $urlParameters
* @return Client
*/
protected function getClient()
{
return new Client([
'base_uri' => 'http://developer.echonest.com/api/v4/',
'timeout' => 5.0,
'headers' => [
'Accept' => 'application/json',
],
'query' => [
'api_key' => 'someKey',
'format' => 'json',
'results' => '10',
'bucket' => 'id:spotify' // I need multiple bucket parameter values with the 'bucket'-name
]);
}
/**
* @param $artist
* @param $title
* @return stdClass|null
*/
public …
Run Code Online (Sandbox Code Playgroud) 我正在使用Date.now()编写一个计时器流,我在理解细节方面遇到了问题.
当我使用switchMap编写流时,它工作正常,并getTestStartTime$()
在启动$开始发出事件后调用.
let start$ = Observable.fromEvent(document.querySelector('#start'), 'click');
let stop$ = Observable.fromEvent(document.querySelector('#stop'), 'click');
let getTestStartTime$ = () => Observable.of(Date.now())
.delay(sampleTime)
.repeat()
.takeWhile(val => true);
let time$ = start$
.switchMap(event => getTestStartTime$())
.map(startTime => Date.now() - startTime)
.map(diff => diff / 1000)
.takeUntil(stop$)
.repeat();
Run Code Online (Sandbox Code Playgroud)
但是当用switchMapTo替换switchMap时,似乎在启动$触发之前调用了该函数.我可以看到这个,因为Date.now()太早调用(它与页面加载的时间相同).
let time$ = start$
.switchMapTo(getTestStartTime$()) // this calls getTestStartTime$ too early
.map(startTime => Date.now() - startTime)
.map(diff => diff / 1000)
.takeUntil(stop$)
.repeat();
Run Code Online (Sandbox Code Playgroud)
谢谢.
我想从数据库中选择比参数中指定的某些指定 UNIX 时间戳更新的行。
该列created_at
是表中的日期时间tracks
。该参数$timestamp
是 UNIX 时间戳(整数)。
所以我尝试使用
Tracks::where('created_at','>=',$timestamp)->get();
它简单地返回所有曲目,无论我指定哪个 $timestamp。
但是我可以看到类型问题,并且我尝试了不同的方法,例如编写“UNIX_TIMESTAMP(created_at)”而不是created_at,但没有运气。
所以我认为有一些优雅而简单的方法可以做到这一点。你能帮助我吗?
我有这个ES6类,在this.categories = data.data
执行错误时失败TypeError: Cannot set property 'categories' of undefined
.
我认为这是由于this
引用了承诺中的上下文.
如何设置this.categories
承诺内部?
谢谢.
class NavbarController {
constructor(bnCategoryService) {
this.categories = []; // This is what should be set with data.data.
this.bnCategoryService = bnCategoryService;
this.getCategories();
}
getCategories() {
this.bnCategoryService.getCategories() // Returns a promise
.success(function(data, status, headers, config) {
console.log(data.data);
this.categories = data.data; // This fails!
})
.error(function(data, status, headers, config) {
console.log('Category service: An error occured getting tags from the server.');
});
}
}
Run Code Online (Sandbox Code Playgroud)