2015-10-16 79 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);  
      }); 
     }   
    }; 
});