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

时间:2015-02-01 13:34:57

标签: javascript angularjs ionic-framework

我正在建立一个离子项目。

这是我的简单控制器:

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未定义

1 个答案:

答案 0 :(得分:2)

您需要在输入字段而不是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>