I am facing an issue with a directive that generates a random number. My goal is to reload or refresh this directive when a checkbox is toggled. Below is the code I have been using, but unfortunately, it's not working as expected.
var app = angular.module('plunker', []);
app.controller('Ctrl1', function($scope, $rootScope) {
$scope.name = 'World';
$scope.updateTemplate = function() {
$rootScope.$broadcast('refresh');
}
});
app.controller('Ctrl2', function($scope) {
$scope.$on('refresh', function (event) {
alert("refreshing");
if ($scope.refresh == 'false') {
$scope.refresh = 'true';
} else {
$scope.refresh = 'false';
}
})
});
app.directive('myElement', function() {
return {
restrict: 'E',
scope : {
refresh : '='
},
template: "<div> Hello World </div>" + Math.random()
}
})
For further reference, please see the Plunkr link: http://plnkr.co/edit/hAPqGoltoZELJ2Cva2sN?p=preview