1

I copied a working solution in another page, but for some reason it does not work; this is a test code:

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

For some reason all that appears is:

{{items[$index].nome}}: {{items[$index].status}} {{items[$index].testo}}

I have the following errors, like if the assignment were never made:

printOperation is not a function

function ($scope) never called.

Like if the angular library were not loaded. What is wrong?

asked Aug 3, 2017 at 13:10

2 Answers 2

4

You are missing ng-app="myapp" and change your ng-repeat too

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

The printOperation is outside the scope of angular. Try to make everything within the scope of angular.

Don't use global variables.

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

cнŝdk
32.2k7 gold badges62 silver badges81 bronze badges
answered Aug 3, 2017 at 13:11
Sign up to request clarification or add additional context in comments.

11 Comments

Thanks, I missed ng-app="myapp". I still have a problem with the calling of printOperation(); unfortunately I cannot call it inside the block as it is triggered by an event; is semplified my code. I am adding the rest now. Unfortunaly events seem no to enter the angular block.
Strangely in the other sample I did not have ng-app="myapp" and it worked all the same, that is why I did not add it in this case. Perhaps there are some details of angular I miss...
At any rate now my code works, for some reason. The event is recieved and the $scope function called. Yet the table is not loaded. DO I need to issue some reload ot something of the kind?
If you want to set something global in angulars scope, refer stackoverflow.com/questions/19383725/….
The reason why your table is not loading is that printOperation won't be recognised as function outside angulars world.
|
1

You should add ng-app="myapp" to launch AngularJS.

Your code is trying to set a variable to Angular code outside the AngularJS world: you can't. But fortunately, you don't really to set a var printOperation: call the function directly when the controller is instanciated.

Notice I changed a bit your table to make it showable.

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>

answered Aug 3, 2017 at 13:18

1 Comment

As I add in my code, I amy not call the function directly as I need another module to load the data to be loaded by websocket. So if I do it upfront, I find nothing. Unfortunately the final part of my code is not called if I set inside the block, for reason I do not know, for that matter. I found this solution on the web.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.