我正在使用 ionic 构建一个项目。
这是我的简单 Controller :
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
函数正在运行,但我无法访问 userName
和 password
变量,因为同时 $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>
关于javascript - AngularJs $scope 在 ionic 项目中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28263269/