AngularJs $scope在离子项目中是未定义的

AngularJs $scope is undefined in ionic project

本文关键字:未定义 子项目 scope AngularJs      更新时间:2023-09-26

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

这是我的简单控制器:

var app = angular.module('myApp', ['ionic']);
app.controller("loginController", ['$scope', function($scope){
    $scope.userName = ""
    $scope.password = ""
    $scope.login = function(){
        //some login things here
    };
}]);

我的网页

 <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>