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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:19:27  来源:igfitidea点击:

How to get value of ng-Model into Controller angular js

javascriptangularjs

提问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 useras model like

但是,如果您仅将其user用作模型,那应该不是问题

<input type="text" ng-model="user" name="username" id="username" />

It'll be added as property in the scopewithout any worries.

它将被添加为属性,而scope无需担心。

But you have added property usernamein user.

但是您usernameuser.

As useris undefinedso the scenario will be undefined.usernamewhich is not permitted.

由于userundefined这样的情景将是undefined.username这是不允许的。

Try to defined useras 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