2015-10-16 57 views
0

HTML:爲什麼ng-bind-html進入無限循環?

<div tooltip="page 1 of 6, 2 of 11, 6 of 19" tooltip-placement="right" ng-bind-html="getField('page_01','Subject','txt_sub_propAdd')"></div> 

角:在控制檯

$scope.currentField='';//Use to hendle Angular Deigest Loop 
    $scope.getField = function(page, section, field) { 
     console.log(i++,section,field); 
     $scope.currentField=$sce.trustAsHtml(self.data.HTMLData[page][section][field]); 
     return $scope.currentField; 
}; 

輸出:

<div tooltip="page 1 of 6, 2 of 11, 6 of 19" tooltip-placement="right" ng-bind-html="getField('page_01','Subject','txt_sub_propAdd')" class="ng-binding"><input type="text" label="Property Address" name="txt_sub_propAdd" value=" " maxlength="60" minlength="30" required="" id=" " class="form-control" regex="[^A-Za-z0-9_-/]" ng-model="subject.txt_sub_propAdd"></div> 

0 "Subject" "txt_sub_propAdd" 
    1 "Subject" "txt_sub_propAdd" 
    2 "Subject" "txt_sub_propAdd" 
    3 "Subject" "txt_sub_city" 
    4 "Subject" "txt_sub_city" 
    5 "Subject" "txt_sub_city" 
    6 "Subject" "txt_sub_state" 
    7 "Subject" "txt_sub_state" 
----- 
----- 
----- 
----- 
till infinity 

在屏幕上輸出210

PS:我必須在屏幕上顯示更多的600個字段(文本框,收音機,複選框)。同時告訴我如何在我的控制器中獲得ng-model值。

+0

你能提供樣品plunkr嗎? – Grundy

+0

'ng-bind'會進入無限循環,因爲你將函數傳遞給它,在每個摘要迭代上進行評估,角度監視函數返回值,所以你在每個函數調用中返回'new'對象,並且觀察者認爲某事更改並重新提高摘要循環:-) – Grundy

+0

您還可以看到[此文檔](https://docs.angularjs.org/error/$rootScope/infdig) – Grundy

回答

0

好的, 我將結果保存在數組中而不是調用函數。此外,我使用這個指令來使我的領域可用於角度。

.directive('compile',function($compile, $timeout){ 
    return{ 
     restrict:'A', 
     link: function(scope,elem,attrs){ 
      $timeout(function(){     
       $compile(elem.contents())(scope);  
      }); 
     }   
    }; 
});