I have been working on creating a valuable service using typescript that involves a basic switch case statement based on values from the collection provided below
[{
book_id: 1,
year_published: 2000
},
{
book_id: 2,
year_published: 2003
},
{
book_id: 3,
year_published: 2007
}]
The logic behind my value is quite simple
module app.values {
export function getAge(year){
let yearDef = null;
switch(true){
case (year > 2003):
yearDef = 'Older than 2003';
break;
case(year < 2003):
yearDef = "Younger than 2003";
break;
default:
yearDef = "It's 2003";
}
}
angular.module("app").value('yearDefService',{
getAge: getAge
});
}
In my controller, I am facing some challenges and not sure about the correct approach. The implementation seems to be failing as I haven't created an interface for the getAge
method, leading to uncertainties on how to call it in the constructor.
// Omitting controllers interface
static $inject = ["dataService","yearDefService"];
constructor(public dataService : app.factory.IDataService,
public yearDefService : any,
public getYear :any
){
var vm = this;
vm.yearThing = dataService.getAllYearThings();
vm.getAge = yearDefService.getAge;
....
When viewing the result on the UI
<ul ng-repeat="year in vm.yearThing">
<li> <b> Year Definition: </b> {{ vm.getAge(year.year_published) }} </li>
</ul>
Unfortunately, the output appears empty and console.log returns undefined. It seems like I may have made mistakes in the implementation. Any guidance or assistance would be truly appreciated.