這似乎是一個福利局問題...
我有簡單的應用程序,顯示待辦事項2所列出,一個文本輸入字段添加新的待辦事項每個列表。
問題1:當試圖添加新待辦事項時,控制器中未定義$ scope.todoText。
問題2:如何將新待辦事項添加到正確的列表中?
代碼:JS
function TodoCtrl($scope) {
$scope.lists = [
{name:'list1',
todos:[
{text:'learn angular', done:true},
{text:'build an angular app', done:false}
]},
{name:'list2',
todos:[
{text:'buy milk', done:false},
{text:'buy fruit', done:false}]}
];
$scope.addTodo = function(listName) {
alert($scope.todoText); // Trying to fix this
// TODO add new todo to listName
$scope.todoText = '';
};
}
HTML
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<ul>
<li ng-repeat="oneList in lists">
<ul>
<hr/>
<h2>=== {{oneList.name}} ===</h2>
<form ng-submit="addTodo({{oneList.name}})">
<input type="text" ng-model="todoText" size="30"
placeholder="add new todo here">
<input type="submit" value="add">
</form>
<li ng-repeat="todo in oneList.todos">
<input type="checkbox" ng-model="todo.done">
<span class="done-{{todo.done}}">{{todo.text}}</span>
</li>
</ul>
</li>
</ul>
</div>
</div>
的jsfiddle這裏:http://jsfiddle.net/supercobra/v8hxg/
工作撥弄請.. –
@ShuklaJayシhttp://jsfiddle.net/v8hxg/16/ – AlwaysALearner
大......它的工作! –