Javascript 如何将 ng-Model 的值转化为 Controller angular js
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34431279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get value of ng-Model into Controller angular js
提问by dede
Please help me to check this part of code.
请帮我检查这部分代码。
<body ng-app = "myApp">
<h1>FORM </h1>
<div ng-controller="myController">
<p><label>Username : </label><input type="text" ng-model="user.username" name="username" id="username" /></p>
<p><label>Email : </label><input type="email" ng-model="user.email"/></p>
<p><label>Verifikasi Email : </label><input type="email" ng-model="user.verify_email" /></p>
<p><label>Password : </label><input type="password" ng-model="user.password" id="password" /></p>
<button type="button" ng-click = "add()" >Sig In</button>
</div>
</body>
In my Javascript:
在我的 Javascript 中:
<script>
var app = angular.module('myApp', []);
app.controller("myController", function($scope){
$scope.user = {};
$scope.add = function(){
$scope.data = [
{ nama : $scope.user.username},
{ email : $scope.user.email},
{password : $scope.user.password } ];
console.log($scope.data);
}
});
Thanks for you all. I already update my script. When I click the button, the console didn't print the data. Why? I think there is something wrong.
谢谢大家。我已经更新了我的脚本。当我单击按钮时,控制台没有打印数据。为什么?我认为有什么不对。
回答by Anik Islam Abhi
You didn't define user
你没有定义 user
But that shouldn't be the problem if you use only user
as model like
但是,如果您仅将其user
用作模型,那应该不是问题
<input type="text" ng-model="user" name="username" id="username" />
It'll be added as property in the scope
without any worries.
它将被添加为属性,而scope
无需担心。
But you have added property username
in user
.
但是您username
在user
.
As user
is undefined
so the scenario will be undefined.username
which is not permitted.
由于user
是undefined
这样的情景将是undefined.username
这是不允许的。
Try to defined user
as object then any property will automically added.
尝试定义user
为对象,然后任何属性将自动添加。
Like this
像这样
$scope.user={};
回答by M. Hanafi
in your HTML you should
在您的 HTML 中,您应该
<body ng-app = "myApp">
<div ng-controller="myController">
<p><label>Username : </label><input type="text" ng-model="user.username" name="username" id="username" /></p>
<p><label>Email : </label><input type="email" ng-model="user.email"/></p>
<p><label>Verifikasi Email : </label><input type="email" ng-model="user.verify_email" /></p>
<p><label>Password : </label><input type="password" ng-model="user.password" id="password" /></p>
<button type="button" ng-click = "add(user)" >Sig In</button>
</div>
</body>
in case of
的情况下
ng-click = "add()"
ng-click = "添加()"
use
用
ng-click = "add(user)"
ng-click = "添加(用户)"
in your controller
在你的控制器中
$scope.add = function(user){
$scope.data = [
{ name : user.username},
{ email : user.email},
{password : user.password }
];
console.log($scope.data);
}); // End add Function