Для этого у нас есть три метода. Они объясняются один за другим ниже.
Первый метод: Создайте файл переменной среды env-var.service.ts и объявите свои динамические значения c переменная. Используйте эту переменную в вашем файле .ts. Вы можете изменить его, когда вам нужно.
env-var.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class EnvVarService {
public route = 'bigfork';
constructor() { }
}
component.ts
import { Component, OnInit } from '@angular/core';
import { EnvVarService } from './env-var.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
itemRef: any;
constructor (
private _env_var: EnvVarService
) {
}
myFunc() {
this.itemRef = db.list(`/plats/` + this._env_var.route + `/lots`);
// ... Further code
}
}
HTML будет таким же, как вы описали.
Второй метод: Создайте JSON, в котором вы можете сохранить все маршруты на основе некоторого условия, а затем получить исправленный маршрут в зависимости от состояния. Вы также можете сохранить это JSON вне этого файла.
component.ts
import { Component, OnInit } from '@angular/core';
import { EnvVarService } from './env-var.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
itemRef: any;
json = { "cond1": "/plats/bigfork/lots", "cond2": "/plats/def/lots", "cond3": "/plats/abc/lots" }
constructor () {}
myfunc(condition) {
let route_value = '';
for (let key in this.json) {
if (key == condition) {
route_value = this.json[key];
}
}
this.itemRef = db.list(route_value);
}
// Rest code will be same
}
HTML будет таким же.
Третий метод: ** Вы также можете принять его от пользователя. ** component.ts
import { Component, OnInit } from '@angular/core';
import { EnvVarService } from './env-var.service';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
itemRef: any;
id: any;
constructor () {}
myFunc() {
this.itemRef = db.list(`/plats/` + this.value + `/lots`);
}
// Rest code will be same
}
component. html
<div class="col-md-3">
<input class="form form-control" style="display:inline" type="text" [(ngModel)]="id">
<button type="button"
routerLink="editlots"
class="btn btn-info"
style="font-size: 15px; text-align: center; margin:0 auto; display: inline;">
View Lots
</button>
</div>