2015-10-19 38 views
1

我有一個角度的RESTful應用程序,我有一個這樣的名單:enter image description hereAngularjs大教堂泄漏裝載模板

每當我點擊這些項目我做一個AJAX調用來檢索數據之一,但我也把這個指令加載不同的模板

.directive('contentItem', function ($compile) { 

    var linker = function(scope, element, attrs, ctrl) { 

      element.html(scope.templates[scope.rec.htmlType]);  

      $compile(element.contents())(scope); 

      scope.$on("$destroy",function() { 
       element.remove();     
      }); 
    } 

    return {    
     link: linker, 
     scope: { 
      rec:'=', 
      templates: '=',    
     } 
    }; 
}) 

,這裏是我的html

<tr ng-repeat="rec in ctrl.correctionRows"> 
<td content-item rec="rec" templates="templates" ></td> 
</tr> 

但後來這裏是我的時間安排怎麼看起來像在Chrome中,CL後舔和開放只有十個這些項目,我的瀏覽器崩潰,顯然內存不足 enter image description here 我甚至試圖強制$(「$銷燬」刪除元素,但無論如何不適用於我。任何想法或建議,將不勝感激。 我甚至嘗試硬編碼的模板和HTML類型,這樣我刪除範圍

  scope: { 
      rec:'=', 
      templates: '=',    
     } 

它並不能使它更好!

現在我已經刪除了用於測試目的的指令,因爲我認爲這是內存泄漏的主要來源,但沒有!現在我只有一個手風琴,裏面有3個內置重複和小控制器!但仍然這是我如何得到輸出 enter image description here

+0

你的模板裏面有什麼? 'scope.templates [scope.rec.htmlType]',遞歸?對範圍$ destroy也是element.remove()沒有意義,它應該自動刪除。 – YOU

+0

沒有模板只是一個包含像不同模板的東西的json文件:{輸入「:」「} – Shilan

+0

@YOU例如我有一個輸入htmlType我加載輸入模板,如果我有複選框,我加載檢查模板等。 – Shilan

回答

0

那麼,畢竟我把angularjs升級到1.4,也改爲templateCache和templateUrl,是的!它不會再泄漏,並且不會使瀏覽器崩潰。 這是我的新指令

.directive('hymn', function($compile) { 
    return { 
     restrict: 'E', 
     templateUrl: function(elem, attrs) { 
      return attrs.templateUrl; 
     } 
    } 
}) 

,自templateUrl具有調用每次從GUI 的錯誤我只是增加了一個有趣的標記在HTML頁面這樣

<td ng-switch="rec.htmlType">   
     <hymn ng-switch-when="input" template-url="app/correction/templates/htmltype/input.html" ></hymn> 
     <hymn ng-switch-when="label" template-url="app/correction/templates/htmltype/label.html" ></hymn> 
     <hymn ng-switch-when="check" template-url="app/correction/templates/htmltype/check.html" ></hymn> 
     <hymn ng-switch-when="datepicker" template-url="app/correction/templates/htmltype/datepicker.html" ></hymn>   
     <hymn ng-switch-when="typeahead-country" template-url="app/correction/templates/htmltype/typeahead-country.html" ></hymn> 
     <hymn ng-switch-default template-url="app/correction/templates/htmltype/input.html" ></hymn> 
    </td> 

似乎並不漂亮,但實際上不泄漏,工作正常!