[英]Angular $scope.var undefined in the controller but present in HTML
This seems to be a newb question... 这似乎是一个新问题......
I have simple app that displays 2 lists of todos and one text input field for adding new todos in each list. 我有一个简单的应用程序,显示2个待办事项列表和一个文本输入字段,用于在每个列表中添加新的待办事项。
Problem 1: When trying to add a new todo the $scope.todoText is undefined in the controller. 问题1:当尝试添加新的待办事项时,$ scope.todoText在控制器中未定义。
Problem 2: how to add the new todo item to the correct list? 问题2:如何将新的待办事项添加到正确的列表中?
Code: JS 代码: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 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 here: http://jsfiddle.net/supercobra/v8hxg/ JSFiddle: http : //jsfiddle.net/supercobra/v8hxg/
Problem 1 : You are trying to use todoText
before even initializing it. 问题1 :您甚至在初始化之前尝试使用todoText
。 Declare it before the alert: 在警报之前声明它:
$scope.addTodo = function(listName) {
$scope.todoText = '';
alert($scope.todoText);
};
Problem 2 : To add a new ToDo to the correct list you can simply pass the index and the new Todo from the HTML: 问题2 :要将新ToDo添加到正确的列表,您只需从HTML传递索引和新Todo:
<form ng-submit="addTodo($index, todoText)">
JS JS
$scope.addTodo = function(idx, todo) {
$scope.lists[idx]['todos'].push(
{text:todo, done:false}
);
};
The model variable todoText
is within the scope of the ng-repeat and the method to add it in parent scope, so you cannot access it. 模型变量todoText
在ng-repeat的范围内,以及在父范围中添加它的方法,因此您无法访问它。 You can pass the collection and the new item into the add method to perform addition of a new TODO something like this 您可以将集合和新项目传递到add方法,以执行添加这样的新TODO
$scope.addTodo = function(list,todoText) {
list.todos.push({text:todoText,done:false});
};
I have updated your fiddle with the fixes. 我已经更新了你的小提琴修复。
http://jsfiddle.net/cmyworld/ADJn7/2/ http://jsfiddle.net/cmyworld/ADJn7/2/
Return the list and the text, as Chandermai explained, because of the scope: 返回列表和文本,正如Chandermai解释的那样,因为范围:
<form ng-submit="addTodo(oneList, todoText)">
<input type="text" ng-model="todoText" size="30"
placeholder="add new todo here">
<input type="submit" value="add">
</form>
Then access it as an object in your controller: 然后将其作为控制器中的对象访问:
sc.addTodo = function(listName, todoText) {
console.log(listName.name);
console.log(todoText);
// TODO add new todo to listName
}; };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.