2016-06-23 86 views
0

在通过AJAX调用将页面动态插入页面后,我得到了与html进行交互的'safe is undefined'错误。例如。当在此html中的选择上选择一个选项时,会抛出此错误,并选择重置为默认值。我猜这事做与不正确添加到范围动态插入的内容,即使我在控制器上$范围进行编译操作角度错误:安全未定义

初始化questionForm:

$scope.questionForm = {}; 

在AJAX调用的成功块所执行的编译操作:

var requestQuestions = response.trim(); 
var template = angular.element('<ng-form name="questionForm">'+requestQuestions+'</ng-form>'); 
var linkFn = $compile(template); 
var element = linkFn($scope); 
$(".form-horizontal").append(element); 

的动态插入HTML(编译已运行后):

<ng-form name="questionForm" class="ng-scope ng-dirty ng-invalid ng-invalid-required"> 
    <div ng-class="{'alert alert-danger': questionForm['question_18'].$invalid &amp;&amp; data.attempted}" class="form-group question alert alert-danger"> 
    <label class="col-sm-3"> 
     1. What sort of role is this 
    </label> 
    <div class="col-sm-9"> 
     <div class="question_box"> 
      <div class="row"> 
       <div class="col-md-11"><select required="required" ng-model="data.application.questions[18]" data-placement="left" data-toggle="tooltip" class="form-control dropdown-question ng-dirty ng-invalid ng-invalid-required" name="question_18"><option value="" disabled="disabled" selected="selected">Please select an option</option><option class="question-item" data-desc="" value="16">Full Time</option><option class="question-item" data-desc="" value="17">Part Time</option><option class="question-item" data-desc="" value="18">Contract</option></select></div><div class="col-md-1 e-padding-5"> 
         <span id="dropdown-tooltip" title="" data-placement="right" data-toggle="tooltip" class="glyphicon glyphicon glyphicon-info-sign hide"> 
         </span> 
       </div> 
     </div> 
     </div> 
    </div> 
<span ng-show="questionForm['question_18'].$error.required &amp;&amp; data.attempted" class="help-block">Required</span> 
</div> 
    <div ng-class="{'alert alert-danger': questionForm['question_19'].$invalid &amp;&amp; data.attempted}" class="form-group question"> 
    <label class="col-sm-3"> 
     2. Why is this role required to be filled? 
    </label> 
    <div class="col-sm-9"> 
    <div class="question_box"><textarea ng-model="data.application.questions[19]" class="form-control ng-pristine ng-valid" name="question_19"></textarea></div> 
    </div> 
<span ng-show="questionForm['question_19'].$error.required &amp;&amp; data.attempted" class="help-block ng-hide">Required</span> 
</div> 

奇怪的是这些表单元素验证实际工作中,如果需要的领域,并在例如验证代码未填写将抛出一个错误:

<div ng-class="{'alert alert-danger': questionForm['question_19'].$invalid &amp;&amp; data.attempted}" class="form-group question"> 

任何帮助将是赞赏。 angular的版本是1.2.16

+0

不需要在表达式中使用'&'。可以使用'&&'。那个错误在哪里被触发?从未见过 – charlietfl

+0

之前的一个嗨@charlietfl必须将其更改为&,当我将其粘贴到编辑器中时,在代码本身中是&&。 assign:function(self,value,locals)var key = indexFn(self,locals); //防止重写Function.constructor会破坏ensureSafeObject检查 var safe = ensureSafeObject(obj(self,locals),parser.text); return safe [key] = value;''10537 of angular.js – robbyc73

+0

ahhh这将有意义 – charlietfl

回答

0
  1. 修改ngclass部分中的表单验证以正确引用子表单元素,例如, :

    纳克级= “{ '警报警报的危险':questionForm.question_18 $无效& & data.attempted}”

2中检查存在一个指令添加手表配合该指令DIV中一个新的子节点,如果子节点的新长度比以前的长度时,编译HTML,更新模型

app.directive("requestQuestions",function($compile,$timeout){ 
return { 
    //restrict:"E", 
    // terminal:true, 
    priority:1000, 
    link:function(scope,element,attrs){ 
     scope.$watch(function() { return element[0].childNodes.length; }, 
      function(newVal,oldVal) { 
       if(newVal !== oldVal) { 
        // if the new value is greater than one , need to remove the existing node 
        if(newVal > 1)element[0].removeChild(element[0].childNodes[0]); 
        $compile(element)(scope); 
       } 
      }); 
    } 
}; 

});