刚开始学习Angular。我正在从O'Reilly的书中复制示例行的行,并收到消息“购物车控制器不起作用”。在下面的示例中,我真的看不到我在做什么错:
HTML:
<!DOCTYPE html>
<html ng-app>
<head>
<title>AngularJS Tutorials</title>
<link rel="stylesheet" href="css/bootstrap.css"></link>
<script type="text/javascript" src="scripts/angular.js"></script>
<script type="text/javascript" src="scripts/myscripts.js"></script>
</head>
<body ng-controller='CartController'>
<div>
<h1>
Your Shopping Cart
</h1>
</div>
<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>
</div>
</body>
</html>
myscripts.js如下:
function CartController($scope) {
$scope.items = [
{title: 'paint pot', quantity: 8, price: 3.95},
{title: 'paint brush', quantity: 12, price: 2.66}
]
$scope.remove = function(index) {
$scope.items.splice($index);
}
}
根据chrome网络控制台,可以正确定位myscripts.js。但是,当我还将控制器的脚本放在html页面上时,也会收到相同的消息。
好像在我的数据数组定义$ scope.items ...之后缺少分号。
现在工作;-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句