未定义的离子形式输入

时间:2015-08-10 19:07:09

标签: javascript angularjs forms ionic-framework ionic

我跟随this tutorial创建一个简单的Ionic来做app。

这是我的 index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="myApp" ng-controller="todoCtrl">

    <ion-pane>
      <ion-header-bar class="bar-stable">
        <h1 class="title">To Do</h1>
      </ion-header-bar>
      <ion-content>
        <div class="item item-input-inset">
          <label class="item-input-wrapper">
          <!-- The actual input tag which is bound to the todoInput using ng-model -->
            <input type="text" placeholder="Add New Item" ng-model="todoInput" size="100">
          </label>
          <!-- Our button thay will call our funtion to add a new todo item -->
          <button class="button button-small" ng-click="todoAdd()">
            Add Item
          </button>
        </div>

        <div ng-repeat="x in todoList">
          <li class="item item-checkbox">
           <label class="checkbox">

           <!-- this is the checkbox element, you will see it is bound to the done setting in the items array -->
           <!-- When clicked it calls the update function to update the item to its done status -->
             <input type="checkbox" ng-model="x.done" ng-click="update()">
           </label>

           <!-- this is a span tag that shows the item text, I am using ng-bind, instead of the span tag we could have used {{x.todoText}} as well -->
           <span>{{x.todoText}}</span>
         </li>
        </div>

        <!-- the remove button will call the remove function and remoave all items that are marked done -->
        <button class="button button-block button-assertive" ng-click="remove()">
         Remove Checked Items
        </button>
      </ion-content>
    </ion-pane>
  </body>
</html>

到目前为止我的 app.js

var app = angular.module('myApp', ['ionic']);

app.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if(window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});

app.controller('todoCtrl', ['$scope', function($scope) {
    //if local storage is null save the todolist to local storage
   if (localStorage.getItem("mytodos") == null){
      $scope.todoList = [ {todoText:'Create app', done:false} ];
      localStorage.setItem("mytodos", angular.toJson($scope.todoList));
    } else {
    //get the todolist from local storage
      $scope.todoList = angular.fromJson(localStorage.getItem("mytodos"));
   }

    // Add an item function
    $scope.todoAdd = function() {
      console.log($scope.todoInput);
    };
}]);

当我点击todoAdd()按钮时,即使我在输入内输入内容,我也会在控制台中获得undefined

我在这里犯了什么错误吗?

1 个答案:

答案 0 :(得分:0)

解决。

HTML

<input type="text" placeholder="Add New Item" ng-model="form.todoInput" size="100">

<button class="button button-small" ng-click="todoAdd(form)">Add Item</button>

JS

$scope.todoAdd = function(formData){
      console.log(formData);
};