gpt4 book ai didi

javascript - AngularJS:这段代码有什么问题吗?

转载 作者:行者123 更新时间:2023-11-28 11:35:50 28 4
gpt4 key购买 nike

我正在读一本 AngularJS 书。我从书中复制了以下代码:

<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<title>Your Shopping Cart</title>
</head>
<body ng-controller='CartController'>
<h1>Your Order</h1>
<div ng-repeat='item in items'>
<span>{{item.title}}</span>
<input ng-model='item.quantity'>
<span>{{item.price | currency}}</span>
<span>{{item.price * item.quantity | currency}}</span>
<button ng-click="remove($index)">Remove</button>
</div>
<script src="js/lib/angular.min.js"></script>
<script>
function CartController($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}
</script>
</body>
</html>

我已经安装了angular.min.js。然而,我从 Chrome 得到了这个输出: enter image description here

显然,代码有问题。然而,作为一个新手,我不知道它在哪里。非常感谢您的帮助。非常感谢。

更新

这是错误输出:

Failed to instantiate module myApp due to:
Error: [$injector:nomod] http://errors.angularjs.org/1.2.10/$injector/nomod?p0=myApp
at Error (native)
at http://localhost:63342/angular-book/js/lib/angular.min.js:6:450
at http://localhost:63342/angular-book/js/lib/angular.min.js:20:260
at http://localhost:63342/angular-book/js/lib/angular.min.js:21:262
at http://localhost:63342/angular-book/js/lib/angular.min.js:29:175
at Array.forEach (native)
at q (http://localhost:63342/angular-book/js/lib/angular.min.js:7:280)
at e (http://localhost:63342/angular-book/js/lib/angular.min.js:29:115)
at $b (http://localhost:63342/angular-book/js/lib/angular.min.js:32:232)
at Zb.c (http://localhost:63342/angular-book/js/lib/angular.min.js:17:431

最佳答案

您必须向您的应用程序“myApp”注册 CartController

一种方法:

angular.module('myApp').controller('CartController',['$scope', function ($scope) {
$scope.items = [
{title: 'Paint pots', quantity: 8, price: 3.95},
{title: 'Polka dots', quantity: 17, price: 12.95},
{title: 'Pebbles', quantity: 5, price: 6.95}
];
$scope.remove = function(index) {
$scope.items.splice(index, 1);
}
}]);

关于javascript - AngularJS:这段代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371575/

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