2016-01-11 34 views
1

仍然在学习与离子在一起的mysql并试图创建一个远程数据库的登录表单,面临一个问题,我得到一个错误“TypeError:无法读取属性'用户名'未定义“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的用户名,我失去了什么吗?

+0

你的代码在哪里抛出错误? – devqon

+0

console.log($ scope.username); –

+0

所以你的意思是'$ scope'在该行中是未定义的?那真的很奇怪 – devqon

回答

0

我无法看到你的ng-controller在代码中写的任何地方。

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

+0

抱歉,我包括控制器文件。 –

+0

好的,那么你可以请创建一个plunkr并分享链接 –

+0

我们可以添加科尔多瓦的东西给plunkr吗? –

相关问题