Angular 6 - функции в ngOnInit не сработают

Вы также можете использовать области запросов, чтобы сделать вещи немного более аккуратными, поэтому вы можете сделать что-то вроде:

Invoice::where('account', 27)->notPaidAt($date)->get();

Затем в вашей модели

public function scopeNotPaidAt($query, $asAt)
{
    $query = $query->where(function ($query) use ($asAt) { 
        $query->where('paid', '=', '0000-00-00')->orWhere('paid', '>=', $asAt); 
    });
    return $query;    
}
0
задан Tenzolinho 16 January 2019 в 16:39
поделиться

2 ответа

Массивы объектов определены неправильно, они должны выглядеть следующим образом, чтобы ваш код работал:

  existingCustomers = [{ customer_name: "a" }, { customer_name: "b" }];

  nonExistingCustomers = [{ customer_name: "c" }, { customer_name: "d" }];

Я протестировал эти массивы, и я напечатал lineStyle в консоли как:

[ 111]

ОБНОВЛЕНИЕ : OP Подтверждено, что определение массива было опечаткой в ​​вопросе. Так что мое единственное другое подозрение - как map или forEach принимают функцию обратного вызова и, следовательно, условие гонки, когда console.log печатает массив. Попробуйте петли for:

  setStyle() {
    // it enters here
    console.log(this.existingCustomers) // has the right value
    console.log(this.nonExistingCustomers) // has the right value

    for (let i = 0; i < this.existingCustomers.length; i++) {
      this.lineStyle.push({ // won't enter here
        "customer": this.existingCustomers[i].customer_name,
        "color": '#000000'
      })
    }

    for (let i = 0; i < this.nonExistingCustomers.length; i++) {
      this.lineStyle.push({ // won't enter here
        "customer": this.nonExistingCustomers[i].customer_name,
        "color": '#ff0000'
      })
    }

    console.log(JSON.stringify(this.lineStyle)) // this is empty
  }
0
ответ дан Aragorn 16 January 2019 в 16:39
поделиться

Убедитесь, что вы реализуете onInit:

import { OnInit } from '@angular/core';
export class Random implements OnInit {
  constructor() { }

  // implement OnInit's `ngOnInit` method
  ngOnInit() { 
     console.log(`OnInit`); 
  }

}
0
ответ дан Kendall Adkins 16 January 2019 в 16:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: