각도 응용 프로그램에 칩 구성 요소를 추가하려고합니다. Typescript, HTML 및 CSS 파일로 작성되었습니다.각도 4의 칩 구성 요소를 대변자로 추가
저는 몇 주 동안 올바르게하기 위해 애 쓰고 있으며 적절한 해결책을 찾지 못했습니다. https://plnkr.co/edit/zvEP9BOktwk6nBojsZQT?p=catalogue
UPDATE :
여기에 내 현재 코드와 Plunkr입니다 내가 선택이라는 문자열 배열이있는 입력을 읽고있다 반영하도록 내 코드를 편집했습니다. 코드라는 문자열 배열을 출력하고 있습니다. 나는 문제가 내 부모 템플릿에서 자녀의 구성 요소를 호출 할 수 아니에요이라고 생각
import {
Component, Input, HostBinding, ElementRef, ViewChild, HostListener, EventEmitter, Output, OnInit, SimpleChanges,
OnChanges, QueryList, TemplateRef, ContentChildren, Directive
} from '@angular/core';
import {ControlValueAccessor} from '@angular/forms';
import {Subject} from "@reactivex/rxjs/dist/es6/Subject";
@Component({
selector: 'chips',
templateUrl: './chips.component.html',
styleUrls: ['./chips.component.scss']
})
export class ChipsComponent implements ControlValueAccessor, OnInit{
@Output() code: string[];
@Input() type = 'text';
@Input() duplicates = false;
@Input() selected: any[];
chips: ['chip1', 'chip2', 'chip3']
chipItemList: any[];
constructor() {
}
ngOnInit() {
console.log("selected in chips component ngOnInit(): " + this.selected);
console.log("code in chips component ngOnInit: " + this.code);
}
addChip($event){
this.selected.push($event.target.value)
$event.target.value = '';
}
/*addChip(addSelectedCode) {
//this.newChips.next(this.addCode);
console.log("addCode in chips component addChip(): " + this.addSelectedCode);
if (!this.chip || !this.duplicates && this.selected.indexOf(this.chip) !== -1) return;
this.selected.push(this.chip);
this.chip = null;
this.propagateChange(this.selected);
this.selectedCodeOutput = this.addSelectedCode;
console.log("selectedCodeOutput in chips component: " + this.selectedCodeOutput);
} */
remove(index: number) {
//this.addSelectedCode.splice(index, 1);
this.propagateChange(this.selected);
this.selectedCodeOutput = this.addSelectedCode;
}
/*
Form Control Value Accessor
*/
writeValue(value: any) {
if (value !== undefined) this.selected = value;
}
propagateChange = (_: any) => {
};
registerOnChange(fn: any) {
this.propagateChange = fn;
}
registerOnTouched() {
}
}
: 아래
내가 함께 일하고 코드입니다.누군가 칩을 추가하기 위해 칩 구성 요소를 가져 오는 데 도움이되는 조언이 있습니까?
감사합니다.
, 당신은 어떻게() 함수 ngOnInit로를 기록하려고하는 거지? 어떤 종류의 가치가 있습니까? 일반적으로 부모 구성 요소와 대화하기 위해 출력을 생성 할 때, 일반적으로 각도의 이벤트 이미 터로 설정하고 특정 유형을 방출합니다. –
답변 해 주셔서 감사합니다. 내가 무엇을 포함하고 있는지보기 위해 그것을 기록하려하고있다. 사실 칩 구성 요소의 ngOnInit에 도달하지도 않습니다. EventEmitter를 사용해 보았지만 원하는 결과를 얻지 못했습니다. –
칩 모듈을 app 모듈의 선언 배열에 추가 했습니까? @Output()을 사용하면 기본적으로 자식이 변경 사항에 대해 부모에게 경고한다는 의미입니다. 서비스 경고 구성 요소에서 선택자 '칩'을 사용하여 자녀를 호출합니다. 실수 (addSelectedCode)를 듣고 있지만 특정 용도로 지시하지는 않았습니다. 자식의 결과물이있을 때 함수를 사용하여 작업하는 것이 좋을 것입니다. sth this (addSelectedCode) = "onSelectedCodeAddition ($ event)"입니다. 또한 앱 모듈에서 서비스 알림 구성 요소를 선언 했습니까? –