javascript - AngularJS:当我已经声明了 $scope 时,获取 $scope is not defined 错误

标签 javascript angularjs

我一直在尝试几种不同的配置,但我的 AngularJS 文件一直收到 $scope is not defined 错误,尽管它已在 Controller 中定义。

我试图让 $scope.idStore$scope.patientTime 在底部的 Controller 部分工作。这些函数从具有特定 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 () {

这是 AppCtrl 函数,没有 $scope 依赖。在该函数中,您要求 $scope 值而不像 function AppCtrl($scope) {

那样在此处注入(inject)它

在假设您有正确的变量引用的情况下解释答案的第二部分。

关于javascript - AngularJS:当我已经声明了 $scope 时,获取 $scope is not defined 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926163/

相关文章:

javascript - 使用 underscore.js 计算对象属性

php - 如何从 PHP 调用 JavaScript 函数?

javascript - 如何使用 Angular 1.5 component() 方法将范围设置为 false

angularjs - Protractor :从表中返回对象数组

javascript - Angularjs - ng-click不触发

javascript - 将 PostGIS 点对象转换为 geoJSON 以进行制图

javascript - 如何通过 Phantom 钱包集成使用 Vanilla JS 和 JSON-RPC 获取 Solana 帐户信息和/或 SOL 余额?

javascript - Angular ui-router 未实例化,给出注入(inject)模块错误

javascript - 如何使用 Passport 和 Node.js 在客户端区分经过身份验证的用户?

angularjs - 如何使用证书进入 NWJS 和 Linux