AngularJs $scope 在 ionic 项目中未定义

AngularJs $scope is undefined in ionic project

我正在用 ionic 构建一个项目。

这是我的简单控制器:

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

app.controller("loginController", ['$scope', function($scope){
    $scope.userName = ""
    $scope.password = ""

    $scope.login = function(){
        //some login things here
    };
}]);

我的HTML

 <ion-content ng-controller="loginController">
            <form class="list" ng-submit="login()">
              <label class="item item-input">
              <span class="input-label">Username</span>
              <input type="text" ng-bind="userName">
              </label>
              <label class="item item-input">
              <span class="input-label">Password</span>
              <input type="password" ng-bind="password">
            </label>
            <button class="button button-block button-positive">
              Login
            </button>
          </form>
</ion-content>

当我点击登录按钮时,Login 函数正在运行,但我无法访问 userNamepassword 变量,因为同时 $scope 未定义

您需要在输入字段上放置 ngModel 指令而不是 ngBind:

<input type="text" ng-model="userName">

完成HTML代码则变成:

<form class="list" ng-submit="login()">
    <label class="item item-input"> 
        <span class="input-label">Username</span>
        <input type="text" ng-model="userName">
    </label>
    <label class="item item-input"> 
        <span class="input-label">Password</span>
        <input type="password" ng-model="password">
    </label>
    <button class="button button-block button-positive">Login</button>
</form>