【问题标题】:why is scope undefined in angular为什么范围在角度中未定义
【发布时间】:2017-02-12 20:17:46
【问题描述】:

所以我正在使用 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);

【问题讨论】:

  • 那是什么规则? $scope.email 应该是用户在应用程序运行时输入的文本。
  • 我误读了代码,因为函数没有关闭,而且格式很差。至于点看这个3分钟视频youtube.com/watch?v=DTx23w4z6Kc
  • 我只是从文本编辑器复制粘贴到这里的代码插入处。很抱歉格式冒犯了您。你知道为什么在 iOS 上 $scope.email 是未定义的,即使文本在字段中?
  • 您什么时候在应用中打印“$scope.email”?
  • 当注册按钮被按下时,它会调用 doLogin() 函数。然后它 alert($scope.email) 应该是用户输入的文本。但它在 iOS 应用上显示未定义。

标签: javascript html ios angularjs


【解决方案1】:

根据官方 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);

【讨论】:

【解决方案2】:

这听起来可能很疯狂..

首先让我感谢所有的回复。我学到了很多关于事情应该如何以及如何将我的代码修复为“标准”的知识。喜欢使用 .对象而不是原始字符串。谢谢。

但是我已经解决了我的问题。使我的字段未定义的事情是我有type='email',如果他们只是输入了电子邮件以外的其他内容,它就会返回未定义。我不知道如何,但这解决了它。我更改了type='text',它解决了我的问题。

【讨论】:

    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-13
    • 1970-01-01
    相关资源
    最近更新 更多