2017-08-03 172 views
1

我在另一個頁面中複製了一個工作解決方案,但由於某種原因它不起作用;這是一個測試代碼:Angular應用程序無法啓動

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
<div ng-controller="MyCtrl" ng-app="myapp"> 
    <table name="commentsTable"> 
     <tr ng-repeat="item in items = obj track by $index"> 
      <td class="plantCell">{{items[$index].nome}}: </td> 
      <td class="statusCell">{{items[$index].status}}</td> 
      <td class="statusCell">{{items[$index].testo}}</td> 
     </tr> 
    </table> 
</div> 
<script language="javascript"> 
    var app = angular.module('myapp', []); 
    var printOperation; 

    function GetFromLocalStorage(key) { 
     var items = localStorage.getItem(key); 
     console.log(items); 
     if (items === null) { 
      console.log("item null"); 
      return null; 
     } else { 
      if (typeof items != "string") { 
       items = JSON.stringify(items); 
      } 
      return items; 
     } 
    } 
    app.controller('MyCtrl', 
     function($scope) { 
      $scope.printComments = function() { 
       $scope.obj = [{ 
        "nome": "first", 
        "status": 1, 
        "testo": "Rottura rullo 1!!!" 
       }, { 
        "nome": "second", 
        "status": 0, 
        "testo": "Rottura rullo fsdfsf!!!" 
       }]; 
       console.log("ricevo evento"); 
       console.log($scope.obj); 
      } 
      console.log("assegno print operation"); 
      printOperation = $scope.printComments; 
     } 
    ); 
    var eventMethod = window.addEventListener ? "addEventListener" : "attachEvent"; 
      var eventer = window[eventMethod]; 
      var messageEvent = eventMethod == "attachEvent" ? "onmessage" : "message"; 
      eventer(messageEvent,function(e) { 
       console.log("ricevo messaggio"); 
       printOperation(); 
      },false); 
</script> 

出於某種原因,所有出現是:

{{項目[$指數] .nome}} {{項目[$指數] .STATUS}} {{項目[$指數] .testo}}

我有以下的錯誤,好像如果轉讓從未提出:

printOperation不是一個函數

函數($ scope)從不調用。

就像角庫不加載一樣。哪裏不對?

回答

4

你缺少ng-app="myapp",改變你的ng-repeat

<div ng-app="myapp" ng-controller="MyCtrl"> 
    <table name="commentsTable"> 
     <tr ng-repeat="item in obj track by $index"> 
      <td class="plantCell">{{items[$index].nome}}: </td> 
     <td class="statusCell">{{items[$index].status}}</td> 
     <td class="statusCell">{{items[$index].testo}}</td> 
    </tr> 
    </table> 
</div> 

printOperation是角的範圍之外。儘量使角度範圍內的一切。

請勿使用全局變量。

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myapp" ng-controller="MyCtrl"> 
 
    <table name="commentsTable"> 
 
     <tr ng-repeat="item in obj track by $index"> 
 
      <td class="plantCell">{{item.nome}}: </td> 
 
      <td class="statusCell">{{item.status}}</td> 
 
      <td class="statusCell">{{item.testo}}</td> 
 
     </tr> 
 
    </table> 
 
</div> 
 
<script language="javascript"> 
 
    var app = angular.module('myapp', []); 
 
    var printOperation; 
 

 
    function GetFromLocalStorage(key) { 
 
     var items = localStorage.getItem(key); 
 
     console.log(items); 
 
     if (items === null) { 
 
      console.log("item null"); 
 
      return null; 
 
     } else { 
 
      if (typeof items != "string") { 
 
       items = JSON.stringify(items); 
 
      } 
 
      return items; 
 
     } 
 
    } 
 
    app.controller('MyCtrl', 
 
     function($scope,$window) { 
 
      $scope.printComments = function() { 
 
       $scope.obj = [{ 
 
        "nome": "first", 
 
        "status": 1, 
 
        "testo": "Rottura rullo 1!!!" 
 
       }, { 
 
        "nome": "second", 
 
        "status": 0, 
 
        "testo": "Rottura rullo fsdfsf!!!" 
 
       }]; 
 
       console.log("ricevo evento"); 
 
       console.log($scope.obj); 
 
      } 
 
      console.log("assegno print operation"); 
 
      $scope.printComments(); 
 
     } 
 
    ); 
 
</script>

+0

謝謝,我錯過了NG-應用= 「MYAPP」。我仍然遇到printOperation()的調用問題; 不幸的是我不能在塊內調用它,因爲它是由一個事件觸發的;是我的代碼semplified。我現在補充其餘的。不幸的事件似乎沒有進入角塊。 –

+0

奇怪的是,在其他示例中,我沒有ng-app =「myapp」,它的工作原理完全相同,這就是爲什麼我沒有在這種情況下添加它。也許有一些角度的細節我想念... –

+0

無論如何,現在我的代碼工作,出於某種原因。接收事件並調用$ scope函數。然而桌子沒有加載。我需要發佈一些類似的重新加載嗎? –

1

您應該添加ng-app="myapp"推出AngularJS。

你的代碼試圖爲AngularJS世界以外的Angular代碼設置一個變量:你不能。但幸運的是,您並不真正設置一個var printOperation:在控制器被實例化時直接調用該函數。

注意我改變了一下你的表格,使其顯示。

var app = angular.module('myapp', []); 
 

 
     function GetFromLocalStorage(key) { 
 
     var items = localStorage.getItem(key); 
 
     console.log(items); 
 
     if (items === null) { 
 
      console.log("item null"); 
 
      return null; 
 
     } else { 
 
      if (typeof items != "string") { 
 
      items = JSON.stringify(items); 
 
      } 
 
      return items; 
 
     } 
 
     } 
 
     app.controller('MyCtrl', function($scope) { 
 
     $scope.printComments = function() { 
 
      $scope.obj = [{ 
 
      "nome": "first", 
 
      "status": 1, 
 
      "testo": "Rottura rullo 1!!!" 
 
      }, { 
 
      "nome": "second", 
 
      "status": 0, 
 
      "testo": "Rottura rullo fsdfsf!!!" 
 
      }]; 
 
      console.log("ricevo evento"); 
 
      console.log($scope.obj); 
 
     } 
 
     console.log("assegno print operation"); 
 
     $scope.printComments(); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    
 
<div ng-app="myapp" ng-controller="MyCtrl"> 
 
    <table name="commentsTable"> 
 
     <tr ng-repeat="item in obj"> 
 
      <td class="plantCell">{{item.nome}}: </td> 
 
      <td class="statusCell">{{item.status}}</td> 
 
      <td class="statusCell">{{item.testo}}</td> 
 
     </tr> 
 
    </table> 
 
</div>

+0

正如我添加我的代碼,我不會直接調用該函數,因爲我需要另一個模塊來加載數據加載websocket。所以,如果我事先做好,我什麼也找不到。不幸的是,我的代碼的最後一部分沒有被調用,如果我在塊內部設置,因爲我不知道,就此而言。我在網上找到了這個解決方案。 –