This is a simple app with - 1 page, 1 view, 2 Controllers and 1 Factory.
The page has two dropdowns where user can choose a report and a set. On selection of either, it will display related table inside ng-include. Report1 & Report2 are different in data & in table structure. So, I have a different html for each report.
At page load, by default, the report selected is Report 1 and Set is Set 1. The data loads fine. The text "one" is also displayed in the console. But, when I select Set 2, nothing happens.
Is it because Report1.html is already loaded ? I think I have to use a shared service, but do not know, how to use it in this case.
index.html:
<div ng-controller="indexCtrl">
<select id="ddlReports" ng-model="ReportId">
<option value="1">Report 1</option>
</select>
<select id="ddlSets" ng-model="SetId" ng-change="ShowReport()">
<option value="1">Set 1</option>
<option value="2">Set 2</option>
</select>
<br/>
<ng-include src="ReportTemplate"></ng-include>
</div>
indexCtrl.js:
$scope.ShowReport = function () {
GlobalVariable.SetId = $scope.SetId;
switch ($scope.ReportId) {
case 1:
$scope.ReportTemplate = "Report1.html";
break;
}
};
Report1.html:
<div ng-controller="Report1Ctrl">
<table>
<thead>
<tr>
<th>#</th>
<th>Mobile No.</th>
<th>Circle</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="response in Report1 track by $index">
<td>{{$index + 1}}</td>
<td>{{response.MobileNo}}</td>
<td>{{response.Circle}}</td>
</tr>
</tbody>
</table>
</div>
Report1Ctrl.js:
//setId is a global variable
console.log("one");
var promise = ReportsFactory.GetReport(GlobalVariable.SetId);
promise.then(function (success) {
if (success.data != null && success.data != '') {
$scope.Report1 = JSON.parse(success.data);
}
else {
console.log(success.data);
}
},
function (error) {
console.log(error);
})
From what I understand, ng-include is not being regenerated and or Report1Ctrl.js is being called again. What should be done to correct that.
SetIdanywhere in your code, and no matter which of the two dropdowns call the function, they are only settingReport1.html.SetId, your controller is looking forsetId, and callingShowReport()may change$scope.ReportTemplatebut that's not going to regenerate theng-includeor callReport1Ctrl.jsagain.....