javascript - Angular 应用程序未启动

标签 javascript angularjs

我在另一个页面复制了一个有效的解决方案,但由于某种原因它不起作用;这是一个测试代码:

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

由于某种原因,所有出现的是:

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

我有以下错误,就像从未进行过分配一样:

printOperation is not a function

function ($scope) never called.

就像没有加载 Angular 库一样。怎么了?

最佳答案

您缺少 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 超出了 Angular 的范围。尽量让一切都在angular的范围内。

不要使用全局变量。

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

关于javascript - Angular 应用程序未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485218/

相关文章:

javascript - 从 Google Maps API for Javascript 获取 TravelTime

javascript - 如何将变量传递给 $.ajax 中的 webservice

angularjs - 无法使用键盘以特定高度导航 angular-ui bootstrap typeahead

javascript - Oauth 使用 Javascript 调用 Twitter API

javascript - angularJS在内存中执行指令并获取innerHTML

javascript - 在 Symfony2 中验证搜索表单后更新 AngularJS 范围

javascript - 为什么 select2 不设置所有数组元素?

javascript - express-jwt-blacklist 抛出错误,如错误 : JWT missing tokenId claimsub in my code

php - 如何通过 PHP 或 Javascript 检查 HTML 代码是否有效

angularjs - AngularJS/Ionic中的深层链接示例