1
我是AngularJS的新手,处于学习阶段。 我想用ng-repeat,让控制器在一个单独的js文件中。 当我运行时,什么也没有显示。它仅仅是空白AngularJS ng-repeat不起作用
的index.html:
<html data-ng-app="">
<head>
<script src="/Scripts/angular.js"></script>
<script src="/controllers/controller.js"></script>
</head>
<body data-ng-contoller='CartController'>
<div data-ng-repeat="item in Items">
<span>{{item.Name}}</span>
<span>{{item.Buyer}}</span>
<input type="text" data-ng-model="item.Quantity" />
<span>{{item.Price}}</span>
<span>Total Price</span>{{item.Quantity * item.Price | currency}}
<button data-ng-click="remove($index)">Remove</button>
</div>
</body>
</html>
controller.js:
function CartController($scope) {
$scope.Items=
[{ Name: "Books", Quantity:12,Price : 20, Buyer: "Wei-Meng Lee" },
{ Name: "Pencils", Quantity: 17, Price : 35, Buyer: "Scott Allen" },
{ Name: "Markers", Quantity: 2, Price: 30, Buyer: "Adam Fazio" }];
$scope.remove = function(index) {
$scope.Items.splice(index, 1);
};
$scope.$apply();
}
删除scope.apply在你的控制器是没有必要 – Dalorzo
当我点击删除按钮,没有真的发生。 – Kanchana
$ scope.Items.remove ... remove不是数组方法。你正在寻找的是拼接功能 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice – Dalorzo