Is there a way for the Controller and Service classes to access the same model without explicitly passing it?
For example:
Controller Class :
import { SearchModel } from "../../models/SearchModel";
import { SearchService } from "../../components/SearchService";
export class SearchController {
public searchModel: SearchModel;
static $inject = ['searchService'];
constructor(private searchService:SearchService) {
this.searchService = searchService;
}
public controllerMethod() {
console.log(this.searchModel.searchKeyword); //This works.
this.searchModel.searchKeyword = "CheckIfSharedObject";
this.searchService.serviceMethod();
}
}
Service Class :
import { SearchModel } from "../../models/SearchModel";
export class SearchService {
public searchModel: SearchModel;
constructor() { }
public serviceMethod() {
// This will not work. i.e this wont print 'CheckIfSharedObject'
console.log(this.searchModel.searchKeyword);
}
}
Model Class :
export class SearchModel {
constructor (
public searchKeyword: string
)
}
In the given example, we want both the controller and service classes to share the model variable searchKeyword
. It currently requires passing the model class object to the service method, but we are looking for a way to achieve this without explicit passing.