[英]How to get value of ng-Model into Controller angular js
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.
我觉得有些不对劲。
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
. 但是您已在
user
添加了属性username
。
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={};
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 =“add()”
use 使用
ng-click = "add(user)"
ng-click =“add(user)”
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.