現在、角度2は非常にクールだと思われますが、まだ問題があります。つまり、「ミニビューコンポーネント」が必要です。これは、ボタンやリンクとして一部のアイテム(顧客など)の小さなビューを意味します。例:販売リスト(私が把握する部分)、そこに顧客IDだけを表示するのではなく、顧客であるフィールドが含まれています。私はそれをモーダルウィンドウ(ブートストラップモーダル)は、顧客の詳細と一緒に表示されます。これはサプライヤーや製品などにも同じです。私は "customermini"コンポーネントを作ろうとしました。私はディレクティブとして顧客コードを渡し、ボタンは "getCustomer"を起動します。これはサービスメソッドを起動して顧客を取得します。これはすべて「平凡」になります。問題は、この「ミニカスタマ」コンポーネントは、「getCustomer」メソッドで取得されたオブジェクトで補間されていることです。しかし、補間が「発生する」たびにエラーが発生します。だから問題は:この状況に最も適したアプローチは何ですか?この「ミニ」コンポーネントは、アプリケーション全体でどこでも利用できるようにしておきたい。なぜなら、リストにはidだけしかないものがたくさんあるからですが、それらを「視認可能」にしたいのです。そしてあなたよりも前に。角2のコンポーネント内部コンポーネント
編集:いくつかのコードを追加:
import {Component} from "angular2/core";
import {ClienteService} from "./clientes.service";
import {Cliente} from "./cliente.model";
import {DateString} from './dateString.pipe';
import {CustomerMiniComponent} from "./customer.mini.component";
@Component({
selector: 'saleslist',
templateUrl: 'app/sales.template.html',
directives: [CustomerMiniComponent],
pipes: [DateString]
})
export class SalesListComponent{
clientes: Cliente[];
constructor(private clienteService: ClienteService){};
lerClientes(){
this.clienteService.getClientes().subscribe(clientes => this.clientes = clientes,
err => {
console.log(err);
});
}
ngOnInit(){
this.lerClientes(); //this is where I get the sales data, don't mind the names, its only a sandbox project, so...
}
}
サービス:
import {Injectable} from 'angular2/core';
import { Http, Response, Headers, RequestOptions } from 'angular2/http';
import {Observable} from "rxjs/Rx";
import {Cliente} from "./cliente.model";
@Injectable()
export class ClienteService {
private url = "http://localhost:8080/api/clientes";
constructor(private http: Http){
}
getCliente (codigo) :Observable<Cliente[]>
{
return this.http.get(this.url + "/" + codigo)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); ;
// var tudo;
// return this.http.get(this.url + "/" + codigo)
// .map((res:Response) =>{tudo = res.json(); var cli = new Cliente; cli=tudo; console.log(cli);})
// .catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
getClientes() :Observable<Cliente[]>
{
return this.http.get(this.url)
.map((res:Response) => res.json())
.catch((error:any) => Observable.throw(error.json().error || 'Server error')); ;
}
}
Customermini:
import {Component, Input, Injectable} from "angular2/core";
import {Cliente} from "./cliente.model";
import {DateString} from './dateString.pipe';
import {ClienteService} from "./clientes.service";
@Injectable()
@Component({
selector: 'customermini',
templateUrl: 'app/customer.mini.template.html',
pipes: [DateString]
})
export class CustomerMiniComponent{
@Input('codigo') clicli: string;
ClienteGot: Cliente[];
Cliente: Cliente;
constructor(private clienteService: ClienteService){};
public lerCliente(){
this.clienteService.getCliente(this.clicli).subscribe(cli => this.ClienteGot = cli);
if (this.ClienteGot != undefined)
{
this.Cliente = this.ClienteGot[0];
console.log(this.Cliente);
}
}
ngOnInit(){
//this.lerCliente();
}
}
そしてcustomerminiテンプレートHTMLに、補間タグがあります顧客であるオブジェクト「Cliente」。
エラーとは何ですか?それがタイプスクリプトであれば、タイプが間違っているオブジェクトを渡すことができないため、何も伝わらずに失敗します。 モーダル(ng2-ui-bootstrap)をどのように構築していますか?私はあなたのやり方のようにオンザフライで生成されたいくつかのモーダルの例を持っています。モーダルコンポーネントを動的に生成した場合、ライブラリを使用してデータを渡すのは面倒です。 – deek
何が起こるのですか?顧客のフィールドにsalesを入力します。このミニカスタマのものを使用して、 – user2960560
のように顧客IDを指示にして質問に質問を追加します。データを操作する場合は、そのディレクティブをコンポーネントに変換する必要があります。 – deek