为什么ng-bind-html会进入无限循环?

时间:2015-10-16 06:15:05

标签: javascript angularjs

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>

Angular:

$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;
};

控制台输出:

    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

屏幕输出:

<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>
  

PS:我要在屏幕上显示600多个字段(文本框,收音机,复选框)。还告诉我如何在控制器中获取ng-model值。

1 个答案:

答案 0 :(得分:0)

好的, 我将结果保存在数组中而不是调用函数。此外,我已经使用此指令使我的字段可用于角度。

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