简体   繁体   English

Angular $ scope.var在控制器中未定义,但存在于HTML中

[英]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.

相关问题 控制器将$ scope.var隐藏在另一个带有角度的控制器中 - Controller hiding $scope.var in another controller with angular Angular View {{var}}正在更新,但是$ scope.var不会改变吗? - Angular View {{ var }} updating, but $scope.var doesn't change? 更改$ scope.var后,Angular.js不会刷新转发器,而只会在刷新后刷新 - Angular.js doesn't refresh the repeater after $scope.var changes but only after refresh AngularJS:如何仅在设置了$ scope.var之后才在视图中设置$ scope.var? - AngularJS: How to set $scope.var in a view only after the $scope.var is set? JADE使用HTML和JSON渲染角度范围var - JADE render angular scope var with HTML and JSON in ng-click表达式在视图中设置$ scope.var的值 - ng-click expression in view setting $scope.var's value $ scope.form。$ error在Angular控制器中是未定义的 - $scope.form.$error is undefined in Angular controller 无法使用Angular从HTML视图获取值到控制器。 解决方案返回$ scope变量未定义 - Unable to get value from HTML view to controller using Angular. Solution returns $scope variable undefined 角范围变量可在html页面上使用,但不能在控制器上使用? - Angular scope variable available on html page but not controller? 角度控制器中的$ scope变量未在HTML中显示 - $scope variable in angular controller not getting displayed in HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM