[英]Angular JS : Failed to instantiate module test due to:
I'm running some Javascript inside a HTML file. 我正在HTML文件中运行一些Javascript。 And I'm trying to use some Angular JS with the function ng-repeat. 我正在尝试将某些Angular JS与ng-repeat函数一起使用。 Each time I run it, I get this error in the console : 每次运行它,我都会在控制台中收到此错误:
angular.js:38 Uncaught Error: [$injector:modulerr]
And when I click on the link within the error message in the console, I get this: 当我在控制台中的错误消息中单击链接时,得到以下信息:
Failed to instantiate module test due to: Error: [$injector:nomod] http://errors.angularjs.org/1.6.4/$injector/nomod?p0=test at https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:6:425 at https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:26:270 at b (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:25:299) at https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:26:44 at https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:42:117 at q (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:7:495) at g (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:41:476) at eb (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:46:44) at c (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:21:373) at Sc (https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js:22:179
Here is my Javascript inside my HTML file: 这是我的HTML文件中的Javascript:
//Get User ID and Trip ID in URL
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
var userid = GetURLParameter('userid');
var tripid = GetURLParameter('tripid');
</script>
<script>
//Get photos URL inside a particular Trip ID from a User ID
var database = firebase.database();
database.ref(`photos/${userid}/trips/${tripid}`).once('value') //
will return you all the photo objects inside the `tripId`
.then(photosSnap => {
var photosObj = photosSnap.val();
var photosUrl = Object.keys(photosObj).map(key =>
photosObj[key].photourl);
// then you can return the array or use it here;
var app = angular.module('test', []);
app.controller('MainCtrl', function($scope) {
$scope.urls = photosUrl;
});
console.log(photosUrl); // will be an array of all the photourls
}).catch(err => alert(err));
And finally my HTML which uses ng-repeat: 最后是使用ng-repeat的HTML:
<div class="spot" ng-app="test" ng-controller="MainCtrl">
<ul>
<li ng-repeat="url in urls">{{url}}</li>
</ul>
</div>
I'm using a controller with scope to loop inside my HTML with an array. 我正在使用带有作用域的控制器来在我的HTML中使用数组循环。 So first I don't know where my error message comes from, and second I'm not sure i'm using the controller and ng-repeat properly. 因此,首先我不知道错误消息来自何处,其次我不确定我是否正确使用了控制器和ng-repeat。
Thanks 谢谢
PS: I included in the head tag of my HTML file this link : PS:我在HTML文件的head标签中包含以下链接:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
EDIT: my Javascript inside my HTML updated (I moved everything inside the controller) 编辑:我的HTML内的Java语言已更新(我将所有内容移动到了控制器内)
The error message is gone, my html just doesn't display any url (though the console shows there are 4 urls in my array photosUrl) 错误消息消失了,我的html只是不显示任何url(尽管控制台显示在我的数组photosUrl中有4个url)
<script>
//Get photos URL inside a particular Trip ID from a User ID
var app = angular.module('test', []);
app.controller('MainCtrl', function($scope) {
var database = firebase.database();
database.ref(`photos/${userid}/trips/${tripid}`).once('value') //
will return you all the photo objects inside the `tripId`
.then(photosSnap => {
var photosObj = photosSnap.val();
var photosUrl = Object.keys(photosObj).map(key =>
photosObj[key].photourl);
// then you can return the array or use it here;
$scope.urls = photosUrl;
console.log(photosUrl); // will be an array of all the photourls
}).catch(err => alert(err));
});
</script>
You should move the database
into your 'MainCtrl' controller. 您应该将database
移到“ MainCtrl”控制器中。
Or, in a perfect world, you would abstract that logic into a seperate factory/service, and inject THAT into your controller. 或者,在理想环境中,您可以将该逻辑抽象到单独的工厂/服务中,然后将其注入到控制器中。
nomod
is Angular's error indicating a module you've asked it to use hasn't been defined. nomod
是Angular的错误,指示尚未定义您要使用的模块。
When you specify ng-app
, AngularJS will automatically try to find the module you've defined with that name to load it into that block. 当您指定ng-app
,AngularJS将自动尝试查找使用该名称定义的模块,以将其加载到该模块中。 But you're defining your module asynchronously, in the callback of a Firebase .once()
. 但是,您正在Firebase .once()
的回调中异步定义模块。 That callback can happen very quickly in some cases, but it's still asynchronous and won't be ready when Angular gets going here. 在某些情况下,该回调可以很快发生,但是它仍然是异步的,当Angular进入这里时它还没有准备好。
angular.module()
definition outside the Firebase callback. 将您的angular.module()
定义angular.module()
Firebase回调。 $scope.$apply()
just below setting $scope.urls =
in the Firebase data callback. 在Firebase数据回调中,在设置$scope.urls =
下方添加$scope.$apply()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.