简体   繁体   中英

Angular app does not start

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?

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> 

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM