2016-11-22 100 views
0

我有一個非常奇怪的問題,只是偶爾出現。有時當我加載我的頁面時,在控制檯中出現以下錯誤。然後,我可以重新加載網站,一切工作正常。AnuglarJs - ReferenceError:錯誤未定義 - 重新加載頁面可解決錯誤

angular.js:12520 ReferenceError: errors is not defined 
    at eval (eval at <anonymous> (https://code.jquery.com/jquery-1.10.2.min.js:4:4994), <anonymous>:1:13) 
    at eval (<anonymous>) 
    at https://code.jquery.com/jquery-1.10.2.min.js:4:4994 
    at Function.globalEval (https://code.jquery.com/jquery-1.10.2.min.js:4:5005) 
    at init.domManip (https://code.jquery.com/jquery-1.10.2.min.js:5:28160) 
    at init.append (https://code.jquery.com/jquery-1.10.2.min.js:5:25581) 
    at init.<anonymous> (https://code.jquery.com/jquery-1.10.2.min.js:5:27127) 
    at Function.access (https://code.jquery.com/jquery-1.10.2.min.js:4:6690) 
    at init.html (https://code.jquery.com/jquery-1.10.2.min.js:5:26701) 
    at https://example.com/app1//Scripts/angular-ui-router.min.js:7:27884 <div ui-view="" class="ng-scope"> 

我不知道確切位置的錯誤發生,但我有一個ng-form,我跟蹤我認爲是源代碼錯誤。奇怪的是,它大部分時間都在工作,如果發生錯誤,我必須重新加載頁面並重新開始工作。有誰之前經歷過這個嗎?

<ng-form name="validationForm"> 

    <ng-form name="companyDetails"> 
     <!-- inputs and other controls here --> 
    </ng-form> 

    <ng-form name="paymentInformation"> 
     <!-- inputs and other controls here -->  
    </ng-form> 

    <div ng-repeat="(key, errors) in validationForm.companyDetails.$error track by $index" class="text-muted"> 
     <p><b>Wrong input in company details:</b></p> 
     <p style="margin-left: 20px;" ng-repeat="e in errors"> 
      - {{e.$name}} 
     </p> 
    </div> 
    <div ng-repeat="(key, errors) in validationForm.paymentInformation.$error track by $index" class="text-muted"> 
     <p> 
      <b> 
       Wrong input in payment information: 
       <script>console.log(errors)</script>: 
      </b> 
     </p> 
     <p style="margin-left: 20px;" ng-repeat="e in errors"> 
      - {{e.$name}} 
     </p> 
    </div> 
</ng-form> 
+0

發佈驅動標記的JS。 – GillesC

+0

在首次加載頁面時,錯誤似乎是「未定義的」。在觸發'ng-repeat'之前,你需要確保它不是'undefined'。 – AndreaM16

+0

您可以嘗試從您的視圖中刪除嵌套。流內容,但沒有表單元素的後代 - 參考https://www.w3.org/TR/html5/forms.html#the-form-element –

回答

0

因爲要創建一個本地JavaScript console.log()這樣的循環中,即使你已經把errorsng-repeat,JS角之外可能不知道它是什麼。

這就是爲什麼你必須errors is undefined

<b> 
    Wrong input in payment information: 
    <script>console.log(errors)</script>: 
</b> 

一個可能的解決方案,這將是:

JS

//Put this in the relevant controller 
$scope.logError = function(error) { 
    console.log(error); 
} 

HTML

<b> 
    Wrong input in payment information: 
    {{logError(errors)}} 
</b> 

這樣你就可以將錯誤傳回給Angular,並且變量的值不應該是未知的。