2017-08-03 175 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。所以,如果我事先做好,我什么也找不到。不幸的是,我的代码的最后一部分没有被调用,如果我在块内部设置,因为我不知道,就此而言。我在网上找到了这个解决方案。 –