我创建了一个调用api来获取数据的服务.我想将其返回给调用组件.
这就是我所拥有的:
SomeComponent() {
someData = string;
constructor(
private _configService: SomeService
)
{
var value = this._configService.getKey('some-key');
console.log(value);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个服务:
export class ConfigService {
response:any;
constructor(private _http:Http) {}
getConfig(): Observable<any>
{
return this._http.get('src/config/config.json')
.map(response => response.json()).publishLast().refCount();
}
getKey(key:string) {
this.getConfig()
.subscribe(
data => {
if (data[key] != 'undefined')
{
return data[key]
} else {
return false;
}
},
error => {
return false;
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是我可以调用方法getKey('some-key'),如果返回的json数组中存在键,则返回数据.如果不是,则返回false.
当这个运行时,我可以看到该对象正在服务中返回,但它没有被返回到组件,而是我得到"未定义".
什么过程正确地返回这个?
我有一个可创建饼图,折线图和其他图形的有角charComponent,我想将它们嵌入到应用程序的各个位置。
我使用选择器:
<chart></chart>
Run Code Online (Sandbox Code Playgroud)
公开图表。
我想做的是在代码中嵌入一个变量,这样我就可以告诉组件我要返回什么图表。
所以像这样:
<chart variable="pie-chart"></chart>
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗,还是有更好的方法呢?
我正忙着为Angular2编写我的第一个管道,我需要一些帮助把它放在一起.
问题: 我有一个api返回一组数字:1-2-03-4-5我想像这样设置这些数字:
<div class="number">1</div>
<div class="number">2</div>
<div class="number">03</div>
<div class="number">4</div>
<div class="number">5</div>
Run Code Online (Sandbox Code Playgroud)
如果这就是PHP我只想去的地方:
$array = explode("-","1-2-03-4-5");
foreach ($array AS $number) {
echo ' <div class="number">'.$number.'</div>';
}
Run Code Online (Sandbox Code Playgroud)
我的目标是以角度做这种事情,所以我创建了一个管道:
import {Pipe} from "angular2/core";
@Pipe({
name:"explode"
})
export class ExplodePipe {
transform(value) {
return some explody stuff here;
}
}
Run Code Online (Sandbox Code Playgroud)
这包含在我的组件中
import {Component, OnInit} from 'angular2/core';
import {RouteParams, ROUTER_DIRECTIVES} from "angular2/router";
import {HelpMenuComponent} from "./help-menu.component";
import {ExplodePipe} from "../Pipes/my.pipes";
@Component({
pipes:[ExplodePipe],
template: `
{{ExplodeMe | explode}}
`
...
Run Code Online (Sandbox Code Playgroud)
export class ViewResultsComponent实现OnInit {ExplodeMe:"1-2-03-4-5"; …
我有一个使用嵌入式关联的资产实体:
/**
* @ORM\Entity
*/
class Asset
{
....
/**
* @ORM\Embedded(class="Money\Money")
*/
private $code;
Run Code Online (Sandbox Code Playgroud)
我想搜索这个类,我的第一直觉是做这样的事情:
public function findOneByCurrencyCode(string $currencyCode)
{
$qb = $this->assetRepository->createQueryBuilder('asset');
$qb->innerJoin('asset.code', 'code')
->where('code.currency = :currency')
->setParameter('currency', $currencyCode);
$result = $qb->getQuery()->getOneOrNullResult();
return $result;
}
Run Code Online (Sandbox Code Playgroud)
然而,这会返回以下内容:
[Semantical Error] line 0, col 65 near 'code WHERE code.currency': Error:
Class Domain\Asset\Asset has no association named code
Run Code Online (Sandbox Code Playgroud)
如何搜索嵌入类?
编辑:
我可以通过做这样的事情得到结果,但是,我觉得这是一个黑客:
$query = "SELECT * from asset where code_currency='BTC';";
$statement = $this->objectManager->getConnection()->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
return $result;
Run Code Online (Sandbox Code Playgroud)