剛開始學習Angular。我嘗試製作一個簡單的購物清單應用程序,但「添加」按鈕不起作用。 當我按下它(ng-submit="addItem()"
)時,什麼也沒有發生。顯然$scope.addItem
無法正常工作。無法通過Angular 1.x添加項目
var myModule = angular.module('list', []);
myModule.controller('ListCtrl', ListCtrl);
function ListCtrl($scope) {
$scope.items = [
{ text: 'Chocolate', done: true },
{ text: 'Potato', done: false },
{ text: 'Banana', done: false },
{ text: 'Water', done: true }
];
$scope.addItem = function() {
$scope.items.push({ text: $scope.itemText, done: false });
$scope.itemText = '';
};
$scope.remain = function() {
var count = $scope.items.length;
angular.forEach($scope.items, function(item) {
count -= item.done;
});
return count;
};
}
.list{
\t width: 400px;
\t margin: 0px auto;
}
.done-true {
text-decoration: line-through;
color: grey;
}
.done-false {
text-decoration: underline;
color: red;
}
<html lang="en" ng-app="list">
<head>
<title>Document 1</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<script src="app.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
\t <div class="list">
\t \t <h2>Shopping List</h2>
\t \t <div class="panel" ng-controller="ListCtrl">
\t \t <span>{{remain()}} item(s) left to buy of {{items.length}}</span>
\t \t \t <table class="table table-striped">
\t \t \t \t <tbody>
\t \t \t \t \t <tr ng-repeat="item in items">
\t \t \t \t \t \t <td><input class="checkbox-inline" type="checkbox" ng-model="item.done"></td>
\t \t \t \t \t \t <td><span class="done-{{item.done}}">{{item.text}}</span></td>
\t \t \t \t \t </tr>
\t \t \t \t </tbody>
\t \t \t </table>
\t \t </div>
\t \t <form ng-submit="addItem()">
\t \t \t <input class="form-control" type="text" ng-model="itemText" size="30" placeholder="Add item to list">
\t \t \t <input class="btn btn-primary" type="submit" value="Add">
\t \t </form>
\t </div>
</body>
</html>
http://codepen.io/ArkadiyS/pen/BzWjvX
你的表格在控制器的div之外 – Sam
@Sam,哦......呃,絕對需要休息一下:)非常感謝! – Archie