6

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:

<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.

4
  • 1
    Try to define the user object before referencing it, like $scope.user = {} ; $scope.data = ... Commented Dec 23, 2015 at 8:15
  • 1
    Thats because you have written user.username, but $scope.user is not defined. Commented Dec 23, 2015 at 8:16
  • 1
    I think you should read about angular tutorials and ap docs .Please Check out docs.angularjs.org/guide/controller Commented Dec 23, 2015 at 8:16
  • Thanks. I try like what you said. I already update it. Can you check it again? Commented Dec 23, 2015 at 8:49

2 Answers 2

7

You didn't define user

But that shouldn't be the problem if you use only user as model like

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

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

But you have added property username in user.

As user is undefined so the scenario will be undefined.username which is not permitted.

Try to defined user as object then any property will automically added.

Like this

$scope.user={};
Sign up to request clarification or add additional context in comments.

Comments

0

in your HTML you should

<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()"

use

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.