OHService.getItem returns null on first loading

Hello, I am creating a widget, that will control my lighting system. I want to have this widget dynamic, so it will load all lights in the predefined premises (eg. room, floor, house, etc.). So I’ve created a new AngularJS controller, that should load all lights in the predefined premises during page load. The issue I am facing is, that during every first load (or reload), OHService.getItem(ItemName) returns null, but when I visit the dashboard with this widget as a second page (firstly open another dashboard and then open the menu and switch to the desired dashboard), the command works as expected.

The whole widget is inside <div ng-controller="ngLightsCtrl" ng-init="initLights(config.place)"> and the corresponding JavaScript code:

function () {
	'use strict';
	angular
		.module('app.widgets.lights', [])
		.controller('ngLightsCtrl', ['$rootScope', '$scope', 'OHService',
			function ($rootScope, $scope, OHService) {

				function getLights (HausePart)
				{
					var itemsInGroup = $scope.itemsInGroup(HausePart.name);
					for (var i = 0; i < itemsInGroup.length ; i++)
					{
						var item = itemsInGroup[i];
						if (item.groupNames.includes('gLight')) {$scope['Lights'].push(item); }
						else {getLights(item);}
					}
				};

				$scope['initLights'] = function (HausePart)
				{
					
					$scope['Lights'] = [];
					var item = OHService.getItem(HausePart); // this command returns NULL
					getLights (item)
					$scope['Modals'] = [];
				};
			}
		]);
})();

Could you please tell me what is wrong? Why the first load of the page doesn’t work?

Thanks
Gregorij

UPDATE:

I´ve discovered, that this is happening because the loading of the items ends after my script is executed. How should I wait for the items to be loaded (e.g. $rootScope.items not to be null)?

Hi gregorij89, I don’t know if this is still an open issue to you? I faced the same problem and after a long time searching for a solution I’ve found

which solved my problem.
Hope that helps.
KR Uwe