With Angular/UI-Router, is it possible to change an element's class in viewA@stateA from viewB@stateA.stateB's controller? WITHOUT changing state?
Here are my state definitions:
.state('map', {
url: '/map',
views:
{
'': {
templateUrl: 'Views/templates/layout.html'
},
......
'kymap@map': {
templateUrl: 'Views/templates/ky.map.html',
controller: 'MapController'
}
}
})
.state('map.districts', {
params: { fips: null },
views:
{
'districtlist': {
templateUrl: 'Views/templates/county.districts.html',
controller: 'CountyDetailsController'
}
}
})
.....
.state('map.distributordistricts', {
params: { distributorid: null, distributorname: null },
views:
{
'distributordistricts': {
templateUrl: 'Views/templates/distributor.districts.html',
controller: 'DistributorDistrictsListController'
}
}
})
.....
EDIT: ADDED RELEVANT SERVICE AND CONTROLLERS CODE
// the Sharing Service
app.factory('HighlightService', function () {
return {
sharedObject: { data: [] }
};
});
// Controller that has the data I want to share with the other controller
app.controller('DistributorDistrictsListController', ['$scope', '$state', '$stateParams', 'DistributorDistrictsService', 'HighlightService',
function ($scope, $state, $stateParams, DistributorDistrictsService, HighlightService) {
$scope.distributorname = $stateParams.distributorname;
$scope.distributordistricts = DistributorDistrictsService.query({ id: $stateParams.distributorid })
$scope.distributordistricts.$promise.then(function (data) {
var countyfpList = [];
for (var i = 0; i < data.length; i++) {
countyfpList.push(data[i].CountyFP);
}
// clear
HighlightService.sharedObject.data.length = 0;
// 2. fill the first array with items from the second
[].push.apply(HighlightService.sharedObject.data, countyfpList);
});
$scope.showDetail = function (id) {
$state.go('map.districtdetails', { districtid: id });
}
}]);
// controller I want to get the data (without activating its state.
// i simply want an ng-class directive's expression to act on it.
/* Map Controller */
app.controller('MapController', ['$scope', '$state', 'HighlightService', function ($scope, $state, HighlightService) {
$scope.showDetail = function (fips) {
$scope.toggleActiveCountyFP = fips;
$state.go('map.districts', { fips: fips });
}
$scope.$watch('HighlightService.sharedObject.data', function (newValue, oldValue) {
console.log(newValue)
}, true);
}]);
END EDIT
From DistributorDistrictsListController, I have a list of County codes that are associated with the id of several SVG path elements in kymap@map... I need to toggle a class on or off on these path's based on whether or not the associated id is in that list.
I attempted it with a service that exposed an object to share, and then used ng-class with an expression like SharingService.SharedObject.MyList.indexOf(id) > -1 but couldn't get it to work.
Also of note, the SVG is created with d3 inside a custom directive that is compiled so that angular directives are working properly.
Can anyone provide some insight, or maybe a simple example?