2017-02-27 59 views
1

全部。兩個組件之間發送字符串格式的問題。雙向數據綁定Angular 2錯誤與字符串

創建子組件

過濾器,panel.component.ts

`@Component({ 
    selector: 'as-filters-panel', 
    templateUrl: './filters-panel.component.html', 
    styleUrls: ['./filters-panel.component.css'] 
}) 
export class FiltersPanelComponent implements OnInit, AfterContentInit { 

    filterByName:string; 
    @Output() searchNameChange = new EventEmitter<string>(); 

    categories : Category[]; 
    subscribe : Subscription; 

    constructor(public categoryService : CategoryService, public router : Router) { 

    } 

    @Input() get filterName() { 
    console.log(this.filterByName); 
    return this.filterByName; 
    } 

    set filterName(val) { 
    this.filterByName = val; 
    this.searchNameChange.emit(this.filterByName); 
    } 

    onNameChange(model: any) { 
    this.filterName = model; 
    }` 

和HTML模板

<input class="filterInput" type="text" [ngModel]="filterName" (ngModelChange)= "onNameChange($event)"> 

在父組件我試圖讓FILTERNAME

<as-filters-panel [(filterName)]="filterNameField" [(selectedSort)]="sortByField"></as-filters-panel> 

並創建filterNameField:string =''; .ts文件 它不適用於字符串,但如果我嘗試發送號碼,效果不錯

+0

爲什麼要顯式觸發事件ngModelChange?提供更多關於你的期望的信息? – Aravind

+0

我想從輸入字段接收數據,需要文本值 –

+0

其中是組件中的輸入字段? – Aravind

回答

0

我發現錯誤。 filterByName:string; @Output()searchNameChange = new EventEmitter();

必須是 filterByName:string; @Output()filterByNameChange = new EventEmitter();