A portion of my webpage is controlled by a specific controller that relies on the global variable $scope.selected. I need to ensure the controller initializes when the page loads and whenever the value of $scope.selected is updated.
.controller ('Details', ['$scope', function ($scope) {
function initializeController() {
$scope.viewProperty = performComplexOperation($scope.selected);
}
$scope.$watch ('selected', initializeController);
initializeController();
}])
<span ng-if="selected.gto" ng-controller="Details">
{{viewProperty}}
</span>
I'm currently calling the initialization function both during startup and whenever $scope.selected changes. However, the current setup doesn't seem optimal. Is there a more elegant solution available?