AngularJS:当我已经声明了$ scope时,获取$ scope未定义错误

时间:2016-03-10 20:04:30

标签: javascript 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文件的。 这样做不正确吗?

1 个答案:

答案 0 :(得分:3)

你在功能上定义了两次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) {

那样注入它

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