0

Scope variable returns undefined value when using inside same Scope function.

index.html

<body ng-app="starter" ng-controller="AppCtrl">
                <form ng-submit="submit()">
                        <span class="input-label">name</span>
                        <input type="text" name="name" ng-model="name">     
                        <input type="submit" name="submit" value="Submit">                    
                </form>
</body>

app.js

angular.module('starter', [])
.controller('AppCtrl', function($scope) {
    $scope.submit = function(){
        alert($scope.name+' scope variable');
            }});

output:

undefined scope variable
GKumar
  • 81
  • 1
  • 1
  • 11

6 Answers6

1

Try this example :

index.php

<body ng-app="starter" ng-controller="AppCtrl">
            <form ng-submit="submit()">
                    <span class="input-label">name</span>
                    <input type="text" name="name" ng-model="start.name">     
                    <input type="submit" name="submit" value="Submit">                    
            </form>

app.js

angular.module('starter', [])
  .controller('AppCtrl', function($scope) {
     $scope.start= {};
     $scope.submit = function(){
       alert($scope.start.name+' scope variable');
        );
Ankur Radadiya
  • 245
  • 3
  • 11
1

$scope.name is coming undefined because it is a scope model declared in UI and no value is assigned to it yet. This is expected behavior. If you want some default values assign those from controller or use an ng-init.

Jithu Rajan
  • 452
  • 3
  • 14
0

Try This...

html files ...

<html>
<head>
   <script src="js/angular.min.js"></script>
</head>

<body ng-app="starter" ng-controller="AppCtrl">
                <form ng-submit="submit()">
                        <span class="input-label">name</span>
                        <input type="text" name="name" ng-model="name">     
                        <input type="submit" name="submit" value="Submit">                    
                </form>
</body>
<script src="app.js"></script>
</body>
</html>

this is your js file

angular.module('starter', [])
.controller('AppCtrl', function($scope) {
    $scope.submit = function(){
        alert($scope.name+' scope variable');
    }
});
Raj
  • 439
  • 1
  • 8
  • 28
0

just pass the modal value in ng-submit like this

1st way ->

  <form ng-submit="submit(name)">
      <span class="input-label">name</span>
      <input type="text" name="name" ng-model="name">     
      <input type="submit" name="submit" value="Submit">                    
   </form>

js file

   $scope.submit = function(value){
       alert(value+' scope variable');
   }});

2nd way ->

or you can do this

   <form ng-submit="submit()">
      <span class="input-label">name</span>
      <input type="text" name="name" ng-model="name">     
      <input type="submit" name="submit" value="Submit">                    
   </form>

js file

your code is fine just check parenthesis it is ok code

  $scope.submit = function(){
     alert($scope.name+' scope variable');
  };

use this working plunker

https://plnkr.co/edit/C7oZck4hpt5xHBKkVGku?p=preview

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
-1

The code looks fine to me there must a syntax error in your controller braces are not completed

I have created a working plunker https://plnkr.co/edit/UGEa1uvS83cJHAcXxYMi?p=preview

angular.module('starter', [])
    .controller('AppCtrl', function($scope) {
        $scope.submit = function() {
            alert($scope.name+' scope variable');
        }
    });
Yatrix
  • 13,361
  • 16
  • 48
  • 78
Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40
-1

Please go through below code --

HTML -

 <body ng-app="starter" ng-controller="AppCtrl">
        <form>
                <span class="input-label">name</span>
                <input type="text" name="name" ng-model="name">     
                <input type="submit" name="submit" value="Submit" ng-click="submit();">                    
        </form>
 </body> 

JS

<script>
    angular.module('starter', []).controller('AppCtrl', function($scope) {
    $scope.name='';
    $scope.submit = function(){
    alert($scope.name+' scope variable');
      };
    });
</script>

Hope it helps you !