I have a `$stateChangeStart` event trigger whenever user navigate between the templates, so in below code when user change a state i am deleting `$scope.filename` that is working as expected but when i go back to that page its throwing a exception and i lose socket.io connection because `$scope.filename` value is still there that is executing if statement. How can i destroy $scope value in this case ?

ctrl.js

```
angular.module('loggingApp').controller('DitCtrl', function($scope, $rootScope, DitFactory, FileSaver, Blob, socket, $uibModal, searchFactory) {
    'use strict';
    $scope.event = [];
    $scope.disabledRecBtn = false;
    $scope.disabledStopBtn = true;
    $scope.showMessage = false;
    var totalCurrentBytes = [];
    $scope.curVal = 4000;
    $scope.maxVal = 37737;

    //ENDS HERE
    var sendMessageFlag = false;
    // Delete File method
    function deleteFile(filename) {
        DitFactory.getFile(filename).then(function(response, $window) {
            console.log('data for download', response);
            var data = JSON.stringify(response.data);
            var blob = new Blob([data], {
                type: 'text/plain;charset=utf-8'
            });
            FileSaver.saveAs(blob, 'server.log');
            socket.emit('stopRecording', $scope.filename);
        });
    }
    socket.on('ditConsumer', function(data) {
        var obj = {
            file: $scope.filename,
            data: data
        }
        $scope.event.push(data);
        jsonToArray();
        socket.emit('messageToFile', obj);
        if (sendMessageFlag === true) {
            return sendMessageToFile(obj);
        }
    });

    function sendMessageToFile(data) {
        if (data.file) {
            socket.emit('startrecording', data);
            $scope.disabledRecBtn = true;
            $scope.disabledStopBtn = false;
            $scope.showMessage = true;
        }
    }
    $scope.stopLogs = function() {
        sendMessageFlag = false;
        $scope.modalInstance = $uibModal.open({
            templateUrl: '/web/global/modal.html',
            controller: 'ModalController'
        });
    };
    $rootScope.$on('downloadFile', function(s, data) {
        deleteFile($scope.filename);
        $scope.disabledRecBtn = false;
        $scope.disabledStopBtn = true;
        $scope.showMessage = false;
    });
    $rootScope.$on('onCancelDelete', function() {
        if ($scope.filename) {
            console.log('oncanceldelete function');
            socket.emit('stopRecording', $scope.filename);
            $scope.disabledRecBtn = false;
            $scope.disabledStopBtn = true;
            $scope.showMessage = false;
        }
    });
    $scope.recordLogs = function() {
        socket.emit('createlogfile');
        socket.on('filename', function(filename) {
            console.log('filename from sever', filename);
            $scope.filename = filename;
            sendMessageFlag = true;
            $scope.disabledRecBtn = true;
            $scope.disabledStopBtn = false;
            $scope.showMessage = true;
        });
    }

    //Search logs code here
    $scope.searchLogs = function() {
        $scope.modalInstance = $uibModal.open({
            templateUrl: '/view/modals/searchModal.html',
            controller: 'SearchController'
        });
        searchFactory.setDitLogs($scope.event);
    }
    $scope.serverFiles = function() {
        $scope.modalInstance = $uibModal.open({
            templateUrl: '/view/modals/serverModal.html',
            controller: 'ServerFilesCtrl'
        })
    }

      $rootScope.$on('$stateChangeStart',
           function(event, toState, toParams, fromState, fromParams){
               if($scope.filename) {
                   console.log($scope.filename);
                   console.log('STATE CHANGED');
                   console.log('destroy on close');
                   socket.emit('stopRecording', $scope.filename);
                   sendMessageFlag = false;
                   $scope.filename = null;
               }
           })
});

```