2017-07-05 42 views
0

我有一個textarea,我試圖將更新推送到該字段。我似乎無法弄清楚如何做到這一點。我成功地讓我的組合框將選定的項目傳遞給dropdownSourceSelected。從那裏,我想: 1)更新textarea的領域 2)清除組合框的值(它使佔位符返回組件角度更新textarea字段

我無法弄清楚如何做到這一點返回不確定我看到引用使用$範圍但是當我嘗試這樣做我得到了約$範圍是未知的錯誤

table-widget.component.html 
     <textarea vertical-align:top ng-model="filterSelection" readonly name="selectedfilters" cols = "50" rows = "1"></textarea> 

table.component.ts 
    public dropdownSourceSelected(selection: string): void { 
    if (this.filterSelection != '') 
     this.filterSelection += '&'; 
    this.filterSelection += "source="; 
    this.filterSelection += selection; 
    console.log('filter selection: ', this.filterSelection); 
    } 
+0

哪個版本的棱角分明是這樣嗎? – DeborahK

+0

我做了這個改變後我們使用Angular 2 – efultz

回答

0

此代碼:

ng-model="filterSelection" 

需求是這樣的:

[(ngModel)]="filterSelection" 

而FYI $scope是AngularJS(v1)。 AngularJS和Angular都有不同的語法,所以任何你在AngularJS中找到的解決方案都不能在Angular中工作。

要在嵌套組件中設置屬性,請在組件中使用@Input屬性,並使用HTML中的屬性綁定來設置該屬性。因此,像這樣:

import { Component, OnChanges, Input, EventEmitter, Output } from '@angular/core'; 

@Component({ 
    selector: 'pm-star', 
    templateUrl: './star.component.html', 
    styleUrls: ['./star.component.css'] 
}) 
export class StarComponent implements OnChanges { 
    @Input() rating: number; 
    starWidth: number; 
    @Output() ratingClicked: EventEmitter<string> = 
      new EventEmitter<string>(); 

    ngOnChanges(): void { 
     this.starWidth = this.rating * 86/5; 
    } 

    onClick(): void { 
     this.ratingClicked.emit(`The rating ${this.rating} was clicked!`); 
    } 
} 

你再設置等級屬性是這樣的:

     <pm-star [rating]='product.starRating' 
          (ratingClicked)='onRatingClicked($event)'> 
         </pm-star> 
+0

如何引用table.component.ts中的字段?如何獲取我在table.component.ts中構建的filterSelection並將其填充到table-widget.component.html的textarea元素中?這是我完全缺失的那個鏈接。 – efultz

+0

我更新了我的答案。如果我可以說...通過使用在stackoverflow中找到的奇數位代碼來「猜測並檢查」,非常難以學習Angular。如果你花了幾個小時並且真正學習了Angular的基礎知識,你會節省大量的時間在你的項目上。如果你有興趣,我有一門課程。 – DeborahK

+0

我很欣賞學習Angular的難度。我已經拿起了一堆,但這些片斷如何連接的基本東西正在踢我。我非常感興趣的課程或一些有意義的教程。 – efultz