Search code examples
javascriptangularjscontrollerangularjs-ng-include

Force ng-include to re-render


I'm trying to make ng-include re render if a $scope variable changes. Example.

<div ng-include src="'assets/courses/templates/editModules/'+currentEditExercise.editTemplate"></div>

My problem is that if the currentExercise.editTemplate changes but it has the same template file. It doesn't re render thus not firing the controller to update the currentExercise model.

on controller:

$scope.currentEditExercise = param.exercise;

on ng-include template: i call a child controller for that template.

$scope.exercise = $scope.$parent.currentEditExercise;

Solution

  • This is happening because ng-include sources are cached.
    This caching can be prevented by adding a random query string to the template url:

    Replace:

    $scope.currentEditExercise = param.exercise;
    

    With:

    $scope.currentEditExercise = param.exercise + '?r=' + Math.random();
    

    So, whenever you change currentEditExercise, the complete url will change, even if param.exercise doesn't.