gpt4 book ai didi

javascript - Angular 应用程序未启动

转载 作者:行者123 更新时间:2023-11-30 08:26:03 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com