2017-01-04 1 views
0

ng를 사용하여 app을 빌드 할 때 productCode 매개 변수를 사용하고 stockTransactionItems 테이블을 표시하는 stockTransactions 뷰로 라우팅하려고합니다. -cli 오류가 발생하지 않지만 Chrome에서 앱으로 이동하면 개발자 콘솔의 아래 오류 메시지가 표시됩니다 ... 거래 내역을 사용할 수 없거나 찾을 수없는 것 같습니다 선택기/구성 요소 ... 나는 app.module에서 StockTransactionItemComponent를 가져 왔고 이전에는 아무런 문제없이 비슷한 일을했습니다 ... 어떤 생각이 잘못되었을 수 있습니까?angular2 : 매개 변수를 사용하여 라우팅 할 때 다른 구성 요소의 템플리트에 구성 요소 템플릿이 표시되는 문제

내 오류 (코드 오류 이하) :

zone.js:388Unhandled Promise rejection: Template parse errors: Can't bind to 'stock-transaction-item' since it isn't a known property of 'tr'. (" ][stock-transaction-item]="stockTransactionItem"> "): [email protected]:63 ; Zone: ; Task: Promise.then ; Value: Error: Template parse errors: Can't bind to 'stock-transaction-item' since it isn't a known property of 'tr'. (" ][stock-transaction-item]="stockTransactionItem"> "): [email protected]:63

주식 transactions.component.html :

<div class="row"> 
    <div class="col-md-6 col-md-push-3"> 
     <div class="col-md-8" *ngIf="stockTransactions"> 
     <h1>Stock Transactions</h1> 
     <table class="table table-hover"> 
      <thead> 
       <tr> 
        <th>Date</th> 
        <th>Type</th> 
        <th>Supplier</th> 
        <th>Qty</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr *ngFor="let stockTransaction of stockTransactions" [stock-transaction-item]="stockTransaction"></tr> 
      </tbody> 
     </table> 
    </div> 
</div> 

주식 transactions.component.ts :

import { Component, OnInit, Input } from '@angular/core'; 
import { ActivatedRoute } from '@angular/router'; 

import { StockTransactionModel } from '../../models/stock-transaction.model'; 
import { RestService } from '../../services/rest.service'; 
@Component({ 
    selector: '[stock-transaction]', 
    templateUrl: './stock-transactions.component.html', 
    styleUrls: ['./stock-transactions.component.css'] 
}) 
export class StockTransactionsComponent implements OnInit { 
    stockTransactions: Array<StockTransactionModel>; 
    productCode: string; 


    constructor(private restService: RestService, private route: ActivatedRoute) { 

    } 


    showStockTransactions(productCode: string) { 
     this.restService.getStockTransactions(productCode) 
      .subscribe(
      (res) => { 
       this.stockTransactions = res; 
       console.log(this.stockTransactions); 
      }, 
      (res) => { 
       console.log("failure " + res); 
      } 
      ); 
    } 

    ngOnInit() { 
     this.productCode = this.route.snapshot.params['productCode']   
    this.showStockTransactions(this.productCode); 
    } 

} 

주식 거래 item.component.html :

<td>{{stockTransactionItem?.Date | date}}</td> 
    <td>{{stockTransactionItem?.TransactionType_Name}}</td> 
    <td>{{stockTransactionItem?.SupplierMaster_Name}}</td> 
    <td>{{stockTransactionItem?.Qty}}</td> 

주식 거래 item.ts : 당신은 stock-transaction-item에 적합한 선택기를 사용하지 않는

import { Component, OnInit, Input } from '@angular/core'; 

import { StockTransactionModel } from '../../models/stock-transaction.model'; 


@Component({ 
    selector: 'stock-transaction-item', 
    templateUrl: './stock-transaction-item.component.html', 
    styleUrls: ['./stock-transaction-item.component.css'] 
}) 
export class StockTransactionItemComponent implements OnInit { 
@Input("stock-transaction-item") stockTransactionItem: StockTransactionModel; 

    constructor() { } 

    ngOnInit() { 
    } 

} 

답변

2

. 반면에 camelCase는 속성 선택 자에 대해 권장됩니다. (부품)

<tbody> 
    <tr *ngFor="let transItem of stockTransactions" [stockTransactionItem]="transItem"></tr> 
</tbody> 

을하고 StockTransactionItemComponent에 변경 :

주식 transactions.component.html : 당신의 템플릿을 변경 지금 잘 작동하고 @PierreDuc

@Component({ 
    selector: '[stockTransactionItem]', //attribute selector 
    templateUrl: './stock-transaction-item.component.html', 
    styleUrls: ['./stock-transaction-item.component.css'] 
}) 
export class StockTransactionItemComponent implements OnInit { 

    @Input() //no need to specify this anymore 
    stockTransactionItem: StockTransactionModel; 

    constructor() {} 

    ngOnInit() {} 

} 
+0

감사합니다! 선택자가 내부에 있어야하는 이유에 대해 설명해 주시겠습니까? – user2094257

+0

[속성 선택자] (https://angular.io/docs/ts/latest/guide/attribute-directives.html)입니다. html 요소의 속성입니다. 괄호없이 괄호를 사용하면 html 요소 인''만 사용하면됩니다. 또한 기본 선택기에 대해서는 [** here **] (http://www.w3schools.com/cssref/css_selectors.asp)를 참조 할 수 있습니다 – PierreDuc

관련 문제