In an angular js application I have a table column defined in main.html as a clickable item. On clicking, it should go to a new customer page, carrying the value of the column cell as customer name. I have a corresponding service defined for the app module in main.js.
Customer page view receives the time series activity data for the selected / clicked customer and supposed to display various charts and tables for the customer. The view and its controller codes are also attached. Also attached are the REST API service.
I expect when the customer is clicked on the table column cell, the control should go to to the setClientFromSelection() under the corresponding service in main.js - where I had the console.log to print the client name - it is not going there!
Not sure what mistake I am making! Any pointer will be much appreciated.
main.html (relevant code):
<tr ng-repeat="data in clientdata track by data.attributes.client" ng-if="data.attributes.Status == statusColor">
<td><a ng-href="#customer" ng-click="setClientFromSelection(data.attributes.client)">{{data.attributes.client.split(".")[0]}}</a></td></tr>
main.js (relevant code):
'use strict';
angular.module('c3App')
.controller('MainCtrl', ['$scope', 'ClientPerf', 'colorTransportService',
function ($scope, ClientPerf, colorTransportService) {
ClientPerf.get()
.success(function(data) {
if (angular.isUndefined($scope.statusColor)) { $scope.statusColor = 'RED'; };
$scope.clientdata = data.payload.array;
});
$scope.$on('healthStatusClicked', function(e, color) {
$scope.statusColor = angular.uppercase(color);
});
}])
.service('clientTransportService', ['$rootScope', function($rootScope) {
var client = '';
var setClientFromSelection = function(clientName) {
console.log("clientName: ", clientName);
client = clientName;
console.log("client: ", client);
$rootScope.$broadcast('clientSelected', client);
}
var getSelectedClient = function() { return client; }
return {
setClientFromSelection: setClientFromSelection,
getSelectedClient: getSelectedClient
};
}])
clientDetails.html view:
<div class="col-lg-6 text-center">
<div class="panel panel-default" ng-style="{'width': '100%'}">
<div class="panel-heading">
<h3 class="panel-title">Usage<linechart data="dailyUsageData" options="dailyUsageOptions" mode=""></linechart></h3>
</div>
<div id="customer_activities_graph" ng-style="{'width': '97%'}"></div>
</div>
<div class=" panel panel-default" ng-style="{'width': '100%'}">
<div class="panel-heading">
<h3 class="panel-title">{{client}} Timeline</h3>
</div>
<div id="container5" ng-style="{'width': '95%', 'margin-left': '2%'}"></div>
</div>
</div>
customer.js controller relevant code:
'use strict';
angular.module('c3App')
.controller('CustomerCtrl', ['$scope', 'DailyUsage4Client', 'clientTransportService',
function ($scope, DailyUsage4Client, clientTransportService) {
$scope.dailyUsageData = [];
$scope.dailyUsageOptions = {
axes: {x: {type: "date", key: "x"}, y: {type: "linear"}},
series: [
{
y: "value",
label: "Activity Count",
color: "#ff7f0e",
type: "column",
axis: "y"
}],
tooltip: {
mode: "scrubber",
formatter: function (x, y, series) {
return moment(x).fromNow() + ' : ' + y;
}
},
stacks: [],
lineMode: "linear",
tension: 0.7,
drawLegend: true,
drawDots: true,
columnsHGap: 5
};
DailyUsage4Client.get()
.success(function (data) {
if (angular.isUndefined($scope.client)) { $scope.client = 'servicemax.com'; };
var dailyUsage = data.payload.array;
for(var k = 0; k < dailyUsage.length; k++) {
$scope.dailyUsageData.push({
date: new Date(dailyUsage[k].attributes.dt.toString().replace(/(\d{4})(\d{2})(\d{2})/, "$2/$3/$1")),
value: dailyUsage[k].attributes.activities
});
};
});
$scope.$on('clientSelected', function(e, client) {
$scope.client = client.split(".")[0];
});
}]);
For sake of completeness, I have the Rest call defined as below:
angular.module('ClientServices', ['ngResource'])
.config(function ($httpProvider) {
$httpProvider.defaults.headers.post['Content-Type'] = 'application/json';
$httpProvider.defaults.cache = false;
})
.factory("DailyUsage4Client", function($http, $rootScope) {
return {
get: function() { return $http.get('http://c3.captora.com/c3rests/c3/usageByDay/{{client}}'); }
}
});