2016-12-15 108 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

相關問題