javascript 未捕获的 ReferenceError:$scope 未定义 - AngularJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20062947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:36:10  来源:igfitidea点击:

Uncaught ReferenceError: $scope is not defined - AngularJS

javascriptangularjs

提问by test

I'm new to using AngularJS. However, why isn't this working?

我刚开始使用AngularJS. 但是,为什么这不起作用?

Upon loading the webpage, I get in the console Uncaught ReferenceError: $scope is not definedon Line 81which is $scope.processForm = function() {Help?

加载网页后,我进入控制台Uncaught ReferenceError: $scope is not defined81这是$scope.processForm = function() {帮助?

 // define angular module/app
 var formApp = angular.module('formApp', []);
 // create angular controller and pass in $scope and $http
 function formController($scope, $http) {
     // create a blank object to hold our form information
     // $scope will allow this to pass between controller and view
     $scope.formData = {};
     // process the form
     $scope.processForm = function() {};
   }
   // process the form
 $scope.processForm = function() {
   $http({
       method: 'POST',
       url: 'process.php',
       data: $.param($scope.formData), // pass in data as strings
       headers: {
         'Content-Type': 'application/x-www-form-urlencoded'
       } // set the headers so angular passing info as form data (not request payload)
     })
     .success(function(data) {
       console.log(data);
       if (!data.success) {
         // if not successful, bind errors to error variables
         $scope.errorName = data.errors.name;
         $scope.errorSuperhero = data.errors.superheroAlias;
       } else {
         // if successful, bind success message to message
         $scope.message = data.message;
       }
     });
 };
<!-- LOAD ANGULAR -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<!-- LOAD BOOTSTRAP CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">

<!-- LOAD JQUERY -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<body ng-app="formApp" ng-controller="formController">
  <div class="container">
    <div class="col-sm-6 col-sm-offset-3">

      <!-- PAGE TITLE -->
      <div class="page-header">
        <h1><span class="glyphicon glyphicon-tower"></span> Submitting Forms with Angular</h1>
      </div>

      <!-- SHOW ERROR/SUCCESS MESSAGES -->
      <div id="messages" ng-show="message">{{ message }}</div>

      <!-- FORM -->
      <form ng-submit="processForm()">

        <!-- NAME -->
        <div id="name-group" class="form-group" ng-class="{ 'has-error' : errorName }">
          <label>Name</label>
          <input type="text" name="name" class="form-control" placeholder="Bruce Wayne" ng-model="formData.name">
          <span class="help-block" ng-show="errorName">{{ errorName }}</span>
        </div>

        <!-- SUPERHERO NAME -->
        <div id="superhero-group" class="form-group" ng-class="{ 'has-error' : errorSuperhero }">
          <label>Superhero Alias</label>
          <input type="text" name="superheroAlias" class="form-control" placeholder="Caped Crusader" ng-model="formData.superheroAlias">
          <span class="help-block" ng-show="errorSuperhero">{{ errorSuperhero }}</span>
        </div>

        <!-- SUBMIT BUTTON -->
        <button type="submit" class="btn btn-success btn-lg btn-block" ng-model="formData.XAlias">
          <span class="glyphicon glyphicon-flash"></span> Submit!
        </button>
      </form>


    </div>
  </div>

  <!-- SHOW DATA FROM INPUTS AS THEY ARE BEING TYPED -->
  <pre>{{formData}}</pre>

回答by KayakDave

Replace the empty $scope.processForminside your controller (function formController($scope, $http)) with the one that's currently outside.

$scope.processForm控制器 ( function formController($scope, $http))内的空白替换为当前位于外部的控制器。

Inside the controller it'll have access to the $scopewhich you injected in.

在控制器内部,它可以访问$scope您注入的内容。