source

angularjs 앱을 어떻게 파괴하죠?

lovecheck 2023. 3. 8. 21:17
반응형

angularjs 앱을 어떻게 파괴하죠?

메모리 누수를 일으키지 않고 각도 있는 애플리케이션을 동적으로 로드/언로드할 수 있어야 합니다.jQuery에서는 다음을 수행할 수 있습니다.$("#elementHoldingMyWidget").remove();적절한 파괴 코드가 실행되어 이벤트핸들러가 언바인드 되는 등

부팅 스트랩이 완료되면 앱이 해체될 수 있다는 내용을 앵귤러에서 찾을 수 없었습니다.

첫 번째 시도는 다음과 같이 rootScope를 파괴하는 것이었습니다.

var rootScope = $("body").scope();   
rootScope.$destroy();

하지만 이 방법은 효과가 없는 것 같고, 작동하더라도 주입기와 서비스가 어떻게 청소될지 잘 모르겠습니다.

어떻게 하면 좋을까요?

AngularJS 1.4.0, $rootScope 사용.$syslog()는 (1.2에서 고장났기 때문에) 다시 동작하고 있습니다.이를 통해 여러 각도 사이를 전환할 수 있습니다.JS 앱:

var appManager = new function () {
    this.currentAppName;
    this.currentApp;

    this.startApp = function (appContainerId, appName) {
        if (this.currentApp) {
            this.destroyApp(this.currentApp, this.currentAppName);
        }
        var appContainer = document.getElementById(appContainerId);
        if (appContainer) {
            this.currentAppName = appName;
            this.currentApp = angular.bootstrap(appContainer, [appName]);
        }
    }

    this.destroyApp = function (app, appName) {
        var $rootScope = app.get('$rootScope');
        $rootScope.$destroy();
    }
}

// Call this when page is ready to rebootsrap app
appManager.startApp('divContainerId', 'app');

사용자에게 화이트 페이지를 표시하지 않고 응용 프로그램을 해체하려면$('body').empty, 내가 먼저$delete()하위 범위를 지정한 다음 모든 속성을 제거합니다.$rootScope:

/*
 * Iterate through the child scopes and kill 'em
 * all, because Angular 1.2 won't let us $destroy()
 * the $rootScope
 */
var scope = $rootScope.$$childHead;
while (scope) {
    var nextScope = scope.$$nextSibling;
    scope.$destroy();
    scope = nextScope;
}

/*
 * Iterate the properties of the $rootScope and delete 
 * any that possibly were set by us but leave the 
 * Angular-internal properties and functions intact so we 
 * can re-use the application.
 */
for(var prop in $rootScope){
    if (($rootScope[prop]) 
        && (prop.indexOf('$$') != 0) 
        && (typeof($rootScope[prop]) === 'object')) {
            $rootScope[prop] = null;
        }
}

2013년 3월 10일 업데이트: $('body').empty();는 앱을 해체하지 않습니다.아직 살아있어.

최초 투고:

이 투고: https://github.com/angular/angular.js/issues/1537#issuecomment-10164971는 (작성 시점에서는) '공식적인' 앱 해체는 없다고 주장하지만, 앱을 가지고 있는 요소를 비우면 된다.

$('body').empty();

만약 이것이 당신이 원하는 것이 아니라면, 앱을 해체하는 임시 해결책으로 다음 절차를 밟을 수 있습니다.https://github.com/angular/angular.js/issues/1537#issuecomment-10184033

언급URL : https://stackoverflow.com/questions/14990480/how-to-destroy-an-angularjs-app

반응형