如何当父组件不使用孩子的组件选择通过从小孩到父组件数据Angular6?

时间:2019-01-31 19:40:31

标签: angular angular6

我正在使用public float calculateMeanFG(int[] histogram, int t) { float sumI = 0; int total = 0; //cumulate the histogram for < 256 for (int i = t; i < 256; i++) { sumI += histogram[i] * i; total = i; } return sumI / total; } public float calculateMeanBG(int[] histogram, int t) { float sumI = 0; //cumulate the histogram for < t for (int i = 0; i < t; i++) { sumI += histogram[i] * i; } return sumI; } public float calculateWeightFG(int[] histogram, int t, int total) { int sum = 0; for (int i = t; i < 256; i++) { sum += histogram[i]; } return sum / total; } public int[] getHistogram(ImageProcessor ip, int height, int width) { byte[] outP = ((byte[]) ip.getPixels()).clone(); int[][] inDataArr = new int[width][height]; int[] histogram = new int[256]; int idx = 0; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // fill in values inDataArr[x][y] = outP[idx]; if (inDataArr[x][y] < 0) { inDataArr[x][y] += 256; } // if histogram[inDataArr[x][y]] += 1; // count grayscale occurrences idx++; } // for x } // for y return histogram; } public int[][] convergeOptThresh(int[][] imgArr, int width, int height) { int BG_VAL = 0; int FG_VAL = 255; int[] histogram = getHistogram(ip, height, width); // total number of pixels int total = imgArr.length; // cumulative hist float sum = 0; for (int i = 0; i < 256; i++) sum += i * histogram[i]; float sumBG = 0; // sum background float weightBG = 0; float weightFG = 0; float varMax = 0; int threshold = 0; for (int t = 0; t < 256; t++) { weightBG = calculateMeanBG(histogram, t); weightBG /= total; weightFG = calculateWeightFG(histogram, t, total); if ((int)weightFG == 0) break; sumBG += (float) (t * histogram[t]); float meanBG = sumBG / t; float meanFG = calculateMeanFG(histogram, t); // calculate between class variance float varBetween = weightBG * weightFG * (meanBG - meanFG) * (meanBG - meanFG); // check if new max found if (varBetween > varMax) { varMax = varBetween; threshold = t; } } IJ.log("optimal threshold: " + threshold); int[][] retArr = new int[width][height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { if (imgArr[x][y] <= threshold) { retArr[x][y] = BG_VAL; } else { retArr[x][y] = FG_VAL; } } } return retArr; } 动态地将子组件添加到父组件,因此不要在父选择器模板或html中使用子选择器。

在这种情况下如何将数据从子组件传递到父组件?

1 个答案:

答案 0 :(得分:0)

所以基本上让我们假设您拥有:

子组件

export class ChildComponent {
   ...
   outputData: EventEmitter<any> = new EventEmitter<any>();
   ...

   triggerChange() {
      // here you just emit some data using event emitter
      this.outputData.emit(someData);
   }
}

然后在父组件中,您可以像这样从EventEmitter访问数据:

父项

export class ParentComponent {

   attachChild() {
      ...
      let componentRef = viewContainerRef.createComponent(componentFactory);
      (<ChildComponent>componentRef.instance).outputData.subscribe(this.outputData.bind(this));
   }

   onDataChange(someData) {
      // here you can access child's data when event emitter triggers
      // so basically it's the same if you subscribed on @Output field from template like (outputData)="onDataChange($event)"
      console.log(someData);
   }
}

希望有帮助。