I have a set of JSON data, which is an array consisting of 50 objects representing individuals. Each object contains specific parameters like id
and lastName
.
In my controller, I retrieve this data using a resolve called EmployeeResolve
and store it in a variable named _this.employees
.
Furthermore, I also obtain a rowNumber
variable from the $state parameters of the previous page, which stores the ID of the record that the user clicked on: _this.rowNum = $stateParams.id;
. Let's assume the id
is 5
.
My objective now is to assign the fifth object (for lack of a better explanation) to a variable so that I can bind it in my HTML using something like {{controller.lastName}}
What would be the correct syntax for retrieving the fifth item from the employees
array?
UPDATE After receiving several helpful comments and answers, I have made progress (now referring to people as packages):
_this.recordNum = Number($stateParams.id);
_this.packages = PackagesResolve;
_this.currentPackage = _this.packages.filter(function(pkg) {
return pkg.id === _this.recordNum;
});
$log.debug('package from filter', _this.currentPackage[0].status);
However, contrary to my expectations, when accessing _this.currentPackage
, it does not contain an object. It appears to be a resource, requiring me to use _this.currentPackage[0].status
as shown in the log statement. Unfortunately, this method doesn't allow for direct binding.
A colleague proposed adjusting my resolve function as follows:
PackagesResolve: function($log, MockDataFactory) {
return MockDataFactory.query({filename: 'packages'}).$promise.then(function(response) {
return response;
});
}
Even with this modification, there seems to be no noticeable change.
To clarify my goal:
The PackagesResolve
function retrieves a JSON array containing 50 objects. My aim is to access the current object when its corresponding row in a table of JSON data is clicked.
It should be noted that this issue cannot be resolved using jQuery or vanilla JavaScript; Angular is the preferred framework for this task.