AngularJS:当我已经声明$scope时,获取$scope不是定义的错误

AngularJS: Getting $scope is not defined error when I already have $scope declared

本文关键字:scope 获取 定义 错误 声明 AngularJS      更新时间:2023-09-26

我一直在尝试几种不同的配置,但是尽管在控制器中定义了AngularJS文件,但我的AngularJS文件不断得到$scope is not defined error

我正在尝试让$scope.idStore$scope.patientTime在底部的控制器部分中工作。这些函数从具有特定 ID 的输入框中提取数据。然后将它们存储为patientID变量。 patientTime将此 ID 与其他时间戳连接起来,然后生成警报消息。

测试表明我在尝试读取idStorepatientTime时出现错误

下面是主要的AngularJS代码:

'use strict';
(function() {
//generate JSON-based list for menu links
    var AppCtrl;
    AppCtrl = function () {
        function AppCtrl($scope) {
            $scope.list = [
                {
                    label: 'Main Menu',
                    icon: 'fa fa-home fa-fw 4x',
                    link: '#/appLaunch',
                    move: function() {
                                 console.log("HOME");
                            }
                },
                {
                    label: 'Patient',
                    icon: 'fa fa-user fa-fw 4x',
                    link: '#/pCredential',
                    move: function(){
                        console.log("PATIENT");
                    }
                },
                {
                    label: 'Technician',
                    icon: 'fa fa-user-md fa-fw 4x',
                    link: '#/tLogin',
                    move: function(){
                        console.log("TECHNICIAN");
                    }
                },
                {
                    label: 'Administrator',
                    icon: 'fa fa-cogs fa-fw 4x',
                    link: '#/aLogin',
                    move: function(){
                        console.log("ADMINISTRATOR");
                    }
                }
            ];
        }
        return AppCtrl;
    }();
// Declare app level module which depends on views and components
angular.module('careApp', [
  'ngRoute',
  'ngMaterial',
  'ngAria',
  'ngAnimate',
  'ngMaterialDatePicker',
  'careApp.appLaunch',
  'careApp.pCredential',
  'careApp.dialog1',
  'careApp.pSurvey',
  'careApp.pTheme',
  'careApp.pThanks',
  'careApp.tLogin',
  'careApp.tMain',
  'careApp.aLogin'
])
.config(['$routeProvider', function($routeProvider) {
    //if given invalid partial, go back to appLaunch.html
  $routeProvider.otherwise({redirectTo: '/appLaunch'});
}])
.controller('AppCtrl', ['$scope', AppCtrl]);
      //declare empty patientID to be accessed by functions
      var patientID = '';
      //grab relevant data from input and add it to patientID
      $scope.idStore = function() {
        var patientFirstInitial = document.getElementById('patientFirstInitial');
        var patientLastName = document.getElementById('patientLastName');
        var aptDate = document.getElementById('aptDate');
        var aptTime = document.getElementById('aptTime');
        //store patientID as global variable
        patientID = patientFirstInitial + patientLastName + aptDate + aptTime;
        console.log('Registered ' + patientID);
      }
      //on time selection, concat to patientID, then throw alert
      $scope.patientTime = function(){
        //concats selected theme to patient ID, then sends to database
        var patientTheme = document.getElementById('');
        patientID += patientTheme;
        alert('Patient ' + patientID + ' has been registered.');
      }
}());

我的怀疑集中在我如何设置这个AngularJS文件。这是不是做错了?

你在函数中AppCtrl定义了两次,而且你也被IIFE右括号搞砸了。它不应该在AppCtrl代码之后。它应该在那里结束。

法典

(function() {
    AppCtrl = function($scope) {
        //why don't have it simple like this
        //inner code will be as is
    };
      // Declare app level module which depends on views and components
    angular.module('careApp', [
      //many dependecies
    ])
    .config(['$routeProvider', function($routeProvider) {
      //if given invalid partial, go back to appLaunch.html
      $routeProvider.otherwise({
        redirectTo: '/appLaunch'
      });
    }])
    .controller('AppCtrl', ['$scope', AppCtrl]);
    //code as is
})();

发生$scope is undefined错误,因为您有

AppCtrl = function () {

hich 是 AppCtrl 函数,没有$scope依赖关系。在该函数中,您要求$scope值,而无需像function AppCtrl($scope) {那样在此处注入它

在解释答案的第二部分时,假设您有正确的可变参考。