0

Service.ts create に get 関数を持つ@Injectableクラスがあります。この関数は 1 つの配列を返し、任意の配列も取得する必要があります。しかし、それは戻りますnull。これを a@Componentに直接記述すると、配列が取得されます。私は何を間違っていますか?

service.ts

@Injectable()
export class Arr_selected {
  private arr = [];
  get(arr_selected: any[]){
      for(let key in arr_selected) {
          if (arr_selected.hasOwnProperty(key)) {
              this.arr.push([key]);
              return this.arr;
          }
      }
      console.log(this.arr);

  }

} 

component.ts

import {Arr_selected} from './service.ts';
export class Component implements DoCheck, OnChanges, OnInit {
....
constructor( public arr_selected: Arr_selected)
.....

ngOnInit{

     let chk = this.ChkBoxValue;

     let arr = [];

/// **this not working Array is null**/////
     arr=(this.arr_selected.get(this.ChkBoxValue));

/// **this is working Array is not null**////
     for (let key in this.ChkBoxValue) {
            if (this.ChkBoxValue.hasOwnProperty(key)) {
              arr.push(this.ChkBoxValue[key]);
         }
       }
        console.log(arr); 
}}
4

2 に答える 2