0
我正在使用ng2選擇在我的模板選擇項目,有一個按鈕名爲「添加」,當點擊應顯示另一個選擇字段與其他類別的項目列表。爲此,我的代碼的最簡單版本如下所示:追加ng選擇按鈕點擊
@Component({
moduleId: module.id,
selector: 'client-user-detail',
templateUrl: `<div #one>
<ng-select #selectRole [allowClear]="true"
[multiple]="true" (data)="refreshValue($event)"
(selected)="onSelectRole($event)"
(removed)="removed($event)"
(typed)="typed($event)"
placeholder="Choose/Search">
</ng-select>
<button [disabled]="isDisable"
class="btn btn-default"
(click)="add()">
<i class=" fa fa-save"></i>
Add
</button>
`,
styleUrls: ['clientuserdetail.component.css']
})
export class TestComponent {
@ViewChild('selectRole') public select: SelectComponent;
@ViewChild('one') d1:ElementRef;
constructor(
private renderer: Renderer
) { }
add() {
let htmlText = `<ng-select #selectRole [allowClear]="true"
[multiple]="true" (data)="refreshValue($event)"
(selected)="onSelectRole($event)"
(removed)="removed($event)" (typed)="typed($event)"
placeholder="Choose/Search">
</ng-select>`;
this.renderer.invokeElementMethod
(this.d1.nativeElement,'insertAdjacentHTML',
['beforeend',htmlText]);
}
}
上述代碼不會創建選擇字段。 我讀過關於組件的解析器在這種情況下可以使用,但我沒有明確的方法來使用它。如果有人能夠簡單地向我解釋如何解決這個問題,我將非常感激。我需要一個關於如何動態添加或附加角度2組件的清晰說明。預先感謝您。
你可以請嘗試用簡單的例子來解釋它嗎?謝謝 –
我編輯了我的文章 – Lejendarm
我不是在推動陣列中的角色。我問的是追加ng-select。例如,如果我正在使用第一個ng-select作爲應用程序1的角色,如果我單擊添加按鈕,則下一個ng-select應該被追加到第一個,第二個應該包含應用程序2的角色等等。它不是關於推動角色。這是關於追加選擇字段。 –