$ scope变量在AngularJS中的$ scope函数内不起作用

时间:2016-12-15 04:58:03

标签: angularjs

当在相同的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

6 个答案:

答案 0 :(得分:1)

试试这个例子:

的index.php

'$cfg['LoginCookieValidity'] = 3600 * 10; //login cookie validity extended upto 10 hours'.

app.js

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

答案 1 :(得分:1)

$ scope.name未定义,因为它是在UI中声明的范围模型,但尚未为其分配任何值。这是预期的行为。如果您希望某些默认值从控制器分配,或使用1.Create a cart

答案 2 :(得分: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');
    }
});

答案 3 :(得分:0)

只需传递 ng-submit 中的模态值,就像这样

第一路 - &gt;

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

第二路 - &gt;

或者你可以这样做

   <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

  

您的代码很好,只需检查括号是否正确

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

使用这个工作的plunker

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

答案 4 :(得分:-1)

代码看起来很好,你的控制器大括号中的语法错误必须完成

我创造了一个工作的plunker   https://plnkr.co/edit/UGEa1uvS83cJHAcXxYMi?p=preview

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

答案 5 :(得分:-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>

希望对您有所帮助!