为什么范围未定义角度

时间:2016-10-04 15:20:55

标签: javascript html ios angularjs

所以我使用ionic来构建混合应用程序。需要明确的是,这与android完美配合!

这是我的登录html

<body ng-app="starter">
    <head>
        <script src="phonegap.js"></script>
    </head>
  <ion-header-bar align-title="center" class="bar-positive" ng-controller="BackBtnCtrl">
          <button class="button" ng-click="goBack()"><<</button>
        <h1 class="title">Push Notifications</h1>
  </ion-header-bar>
    <ion-content>
        <div class="list">
        <form style="display: block; margin: 100px">
                <label class="item item-input">
                    <input type="text" ng-model="name" placeholder="Name">
                </label>
                <label class="item item-input">
                    <input type="password" ng-model="password" placeholder="Password" style="text-align: center">
                </label>
                <label class="item item-input">
                    <input type="email" ng-model="email" placeholder="Email" style="text-align: center">
                </label>
                <label class="item item-input">
                    <input type="text" ng-model="company" placeholder="Company Name" style="text-align: center">
                </label>
        <button class="button button-block button-positive" type="submit" ng-click="doLogin()">Register</button>
        </form>
        </div>
    </ion-content>
</body>

现在我的app.js我将控制器的离子内容声明为:

.state('login', {
        url: '/login',
        templateUrl: 'templates/login.html',
        controller: 'LoginCtrl'
 })

当我访问Android应用程序中的$scope.email时,它会正确返回。但是当我在iOS应用中访问$scope.email时,我得到了未定义。有没有人听说过这个问题?

控制器:

.controller('LoginCtrl', function($scope, $rootScope, $http, $state, $ionicPlatform) {
    if(window.localStorage.getItem("loggedIn") == undefined || window.localStorage.getItem("loggedIn") == null) {
        // This function only gets called when the register button gets hit. It posts to the server to store the users registration data
        $scope.doLogin = function() {

      if ($scope.name == undefined || $scope.password == undefined|| $scope.email == undefined|| $scope.company == undefined) {
        window.plugins.toast.showWithOptions(
          {
            message: 'Please Fill in ALL Fields',
            duration: 'long',
            position: 'middle'
          });
          alert($scope.email);
          alert("returning");
          return;
      }
      alert($scope.email);

2 个答案:

答案 0 :(得分:0)

根据Angular官方文档,你应该像这样声明控制器:

myApp.controller('GreetingController', ['$scope', function($scope) {
  $scope.greeting = 'Hola!';
}]);

因此,请尝试按以下方式更改代码:

.controller('LoginCtrl', ['$scope', '$rootScope', '$http', '$state', '$ionicPlatform', function($scope, $rootScope, $http, $state, $ionicPlatform) {
    if(window.localStorage.getItem("loggedIn") == undefined || window.localStorage.getItem("loggedIn") == null) {
        // This function only gets called when the register button gets hit. It posts to the server to store the users registration data
        $scope.doLogin = function() {

      if ($scope.name == undefined || $scope.password == undefined|| $scope.email == undefined|| $scope.company == undefined) {
        window.plugins.toast.showWithOptions(
          {
            message: 'Please Fill in ALL Fields',
            duration: 'long',
            position: 'middle'
          });
          alert($scope.email);
          alert("returning");
          return;
      }
      alert($scope.email);

答案 1 :(得分:0)

这可能听起来很疯狂..

首先让我感谢所有回复。我已经了解了应该如何做以及如何将代码修改为“标准”。喜欢用。对象而不是原始字符串。谢谢。

但是我已经解决了我的问题。让我的字段未定义的事情是,我有type='email',如果他们只是输入了电子邮件以外的东西,它就会返回未定义。我不知道怎么做,但是修好了。我更改了type='text'并解决了我的问题。