TypeError:无法读取属性'用户名'未定义的

时间:2016-01-11 08:38:25

标签: angularjs ionic

仍然在与mysql一起学习离子并尝试使用远程数据库创建一个登录表单,面临一个问题,我收到了错误" TypeError:无法读取属性'用户名'未定义"

以下是我的代码

的login.html

<ion-header-bar align-title="center" class="bar-balanced">
  <h1 class="title">Onseral</h1>
</ion-header-bar>
<ion-view>
  <ion-content class="padding has-header">
    <ion-list>
       <ion-item>
         <input type="text" ng-model="username" placeholder="Username">
       </ion-item>
       <ion-item>
         <input type="password" ng-model="password" placeholder="Password">
       </ion-item>
     </ion-list>

     <button nav-clear class="button button-block button-balanced" ng-click="LogIn()">Login</button>
  </ion-content>
</ion-view>

controller.js

angular.module('ionicApp.controllers', [])
 .controller('AppCtrl', function($scope) {
 })

.controller('LoginCtrl', function($scope, $state, $http, $ionicPopup) {
  $scope.LogIn = function() {
    var request = $http({
        method: "post",
        url: "http://www.mywebsite.com/api/login.php",
        data: {
          username: $scope.username,
          password: $scope.password
        },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
      });

      /*Successful HTTP post request or not */

      request.success(function (data){

        if (data == '1'){
          console.log('test success');
          }
         else {
         console.log('test failed');
         console.log($scope.username);
      }
    })
  }
});

app.js

angular.module('ionicApp', ['ionic','ionicApp.controllers'])

.config(function($stateProvider, $urlRouterProvider) {

 $stateProvider

.state('app', {
  url: "/app",
  abstract: true,
  templateUrl: "templates/menu.html",
  controller: 'AppCtrl'
})

    .state('login', {
     url: "/login",
     templateUrl: "templates/login.html",
     controller: 'LoginCtrl'
 });
  // if none of the above states are matched, use this as the fallback
 $urlRouterProvider.otherwise('/login');
});

它无法从login.html获取用户名,我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

我无法在代码中的任何地方看到你的控制器。

您的ng-model绑定到您使用的控制器,因此您的视图中没有任何控制器,控制器中用户名的值是未定义的。

相关问题