2016-12-15 112 views
0

当在相同的Scope函数中使用时,Scope变量返回未定义的值。

的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'); 
      }}); 

输出:

undefined scope variable 
+1

将ngModel作为参数传递给ngSubmit的submit()或初始化控制器内的$ scope.name –

+1

我测试了你的代码,它工作的很好.http://jsfiddle.net/Lvc0u55v/13278/ – Ved

+0

@Ved说的都是没问题,你的代码.....但我不能看到关闭}你的函数提交,并有一个额外的)在你的app.js.这可能会导致您的问题 –

回答

1

尝试这个例子:

的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'); 
     ); 
-1

代码看起来没什么问题就必须在你的控制器括号语法错误,未完成

我创建了一个工作的重击者 https://plnkr.co/edit/UGEa1uvS83cJHAcXxYMi?p=preview

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

在此包含相关修复和任何解释,不要只发布链接。 – Yatrix

+0

我编辑了我的答案,请检查。 –

+0

我没有投票给你。 =) – Yatrix

-1

请在下面的代码经过 -

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> 

希望它可以帮助你!

0

尝试......

HTML文件...

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

这是你的JS文件

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

只是通过在模态值NG提交这样

一号路 - >

<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文件

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

第二个办法 - >

,或者你可以做到这一点

<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文件

你的代码是罚款只是检查括号它是确定代码

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

使用该工作plunker

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

1

$ scope.name来了不确定,因为它是在UI声明的范围模型,并没有值分配给它。这是预期的行为。如果您想要一些默认值从控制器分配或使用ng-init

相关问题