Implement function in child element

0

I have my template in a parent element and my child elements that extend the parent element class.

import { Component } from '@angular/core';

@Component({
  selector: 'ngx-cadastro',
  template: '
  <div class="container btns-listagem">
    <div class="row campoPesquisa">
      <div class="col-xs-12 col-sm-6 div-btnPesquisa">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="Pesquisar" name="pesquisar" [(ngModel)]="pesquisar">
          <span class="input-group-btn">
            <button class="btn btn-secondary" id="btn-prequisa" type="button" data-toggle="tooltip" data-placement="top" title="Pesquisar">
              <i _ngcontent-c30="" class="ion-search"></i>
            </button>
          </span>
        </div>
      </div>
    <div class="col-xs-12 col-sm-6 btns-funil-novo">
      <span class="input-group-btn">
        <button type="button" class="btn btn-info" id="btn-funil" data-toggle="tooltip" data-placement="top" title="Filtro">
          <i _ngcontent-c30="" class="ion-funnel"></i>
        </button>
        <button (click)="novoCadastro()" type="button" class="btn btn-success border-right-0" data-toggle="tooltip" data-placement="top" title="Novo" id="btn-novo">
          <i _ngcontent-c30="" class="ion-plus-round"></i>
        </button>
      </span>
    </div>
  </div>
  </div>
  <router-outlet></router-outlet>',
})

export class CadastroComponent {}

Example child element:

import { Component, OnInit } from '@angular/core';

import { CidadeService } from './cidade.service';
import { CadastroComponent } from '../cadastro.component';

@Component({
    selector: 'ngx-lista-cidades',
    templateUrl: './cidade.component.html',
    providers: [ CidadeService ],
})

export class ListaCidadesComponent extends CadastroComponent implements OnInit {

    private cidades: object[];
    private coluna: string;

    constructor(private cidadeService: CidadeService) {
        super();
    }


    ngOnInit() {
        this.ListaTodasCidades();
    }

    novoCadastro() {
        console.log('aqui');
    }

    private ListaTodasCidades() {

        this.cidadeService.TodasCidades().then((response: object[]) => {
            this.cidades = response.sort(function(a: any, b: any) {
                return a.NOME < b.NOME ? -1 : a.NOME > b.NOME ? 1 : 0;
            });
        }, (error) => {
            console.log(error);
        });
    }

    private ordena(coluna) {

        if (this.coluna === coluna) {
            this.cidades.reverse();
        } else {
            this.cidades.sort((a, b) => {
                return a[coluna] < b[coluna] ? -1 : a[coluna] > b[coluna] ? 1 : 0;
            });
        }
        this.coluna = coluna;
    }
}

If you look at my parent element template has a button with the newCast function, would I have to implement this function in my child element?

    
asked by anonymous 17.01.2018 / 11:51

2 answers

1

Dude I do not understand much the reason of CadastroComponent's extension, if there is something to be reused you should create a separate component and use it. Regarding the execution of a method of the parent class it looks at @Output() and EventEmitter . Here's an example on the angled page that shows how link

    
19.01.2018 / 20:57
0

You need to create an abstract function in the parent class for you to implement the code in daughters. For example:

@Component({
  selector: 'ngx-cadastro',
  template: '
    ...
        <button (click)="novoCadastro()" type="button" class="btn btn-success border-right-0" data-toggle="tooltip" data-placement="top" title="Novo" id="btn-novo">
          <i _ngcontent-c30="" class="ion-plus-round"></i>
        </button>

  ...',
})

export class CadastroComponent {
    abstract novoCadastro();
}

Now in the child class you just need to implement the code:

export class ListaCidadesComponent extends CadastroComponent implements OnInit {

    ...

       novoCadastro() {
        console.log('aqui');
    }

}

Remembering that it only works in the sense Pai > Son, never the other way around.

    
07.03.2018 / 18:29