我一直在尝试几种不同的配置,但我的 AngularJS 文件一直收到 $scope is not defined 错误
,尽管它已在 Controller 中定义。
我试图让 $scope.idStore
和 $scope.patientTime
在底部的 Controller 部分工作。这些函数从具有特定 ID 的输入框中提取数据。然后将它们存储为 patientID
变量。 patientTime
将此 ID 与附加时间戳连接起来,然后生成一条警报消息。
测试表明我在尝试读取 idStore
或 patientTime
时遇到错误
下面是 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) {
在假设您有正确的变量引用的情况下解释答案的第二部分。
关于javascript - AngularJS:当我已经声明了 $scope 时,获取 $scope is not defined 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35926163/