ng-model在控制器中未定义 [英] ng-model is undefined in controller
问题描述
我正在使用离子,我有以下观点:
I'm using ionic and I have the following view:
<ion-view hide-nav-bar="true" ng-controller="loginController" class="login-view">
<ion-content class="padding">
<div class="row row-center">
<div class="col">
<div id="logo"></div>
<form>
<div class="list">
<label class="item item-input">
<input type="text" placeholder="Membership No" ng-model="membershipNo">
</label>
<label class="item item-input">
<input type="password" placeholder="Password" ng-model="password">
</label>
</div>
<button class="button button-block button-positive button-login" ng-click="login()">
Login
</button>
</form>
</div>
</div>
</ion-content>
</ion-view>
我的控制器:
app.controller('loginController', ['$scope', '$localstorage',
function($scope, $localstorage) {
$scope.membershipNo;
$scope.password;
$scope.login = function () {
console.log("User logged in with membership no: " + $scope.membershipNo +
"\n and password: " + $scope.password);
}
}
]);
我不明白的是,当我点击按钮时,正确调用登录功能。此外,如果在控制器中我将 $ scope.membershipNo
设置为类似Banana Pancake的内容,视图实际上会更新。
What I don't understand, is that when I click the button, the login function is called correctly. Also, if in the controller I go and set $scope.membershipNo
to something like "Banana Pancake", the view actually updates.
然而,当登录功能实际运行时,会说memberNo和密码未定义。我对Angular和Ionic很新,所以我知道这可能是一些n00b的错误......
Yet when the login function actually runs, is says that membershipNo and password are undefined. I'm fairly new to Angular and Ionic so I know this is probably some n00b mistake...
推荐答案
我确切地知道了最近同样的问题,这可能是一个解决方案:
https://stackoverflow.com/a/22768720/552936
I had exact the same issue recently and that's probably a solution: https://stackoverflow.com/a/22768720/552936
修改后的报价:
如果你使用ng-model ,你必须有一个点。
让你的模型指向object.property
,你会很高兴。
控制器
$scope.formData = {};
$scope.login = function () {
console.log("User logged in with membership no: " + $scope.formData.membershipNo +
"\n and password: " + $scope.formData.password);
}
模板
<input type="text" placeholder="Membership No" ng-model="formData.membershipNo">
<input type="password" placeholder="Password" ng-model="formData.password">
这篇关于ng-model在控制器中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!