Javascript 如何在角度视图中显示阵列中的随机对象?
我尝试将数组中的随机对象的信息显示到DOM中的NG Repeat场景中: 首先,这里是控制器的代码,它只是创建数组并填充对象:Javascript 如何在角度视图中显示阵列中的随机对象?,javascript,angularjs,arrays,random,Javascript,Angularjs,Arrays,Random,我尝试将数组中的随机对象的信息显示到DOM中的NG Repeat场景中: 首先,这里是控制器的代码,它只是创建数组并填充对象: // We check the businesses in the 500m to display a random wide range ad // First we get the coordinates of the user database.once('value', function(snapshot) { var lat50 = sna
// We check the businesses in the 500m to display a random wide range ad
// First we get the coordinates of the user
database.once('value', function(snapshot) {
var lat50 = snapshot.val().lat;
var long50 = snapshot.val().long;
var rootRef50 = firebase.database().ref();
var geoFireRef50 = rootRef50.child("geobusiness");
var restaurantsRef50 = rootRef50.child("businessarround");
var geoFire50 = new GeoFire(geoFireRef50);
// Then we make a call for Geofire to check all businesses arround
var geoQuery50 = geoFire50.query({
center: [lat50, long50],
radius: 0.5
});
// This is the array that contains the results below
var matches50 = [];
console.log(matches50);
// Listen to every businesses in our query...
geoQuery50.on("key_entered", function(key) {
// ... and look them up by key ...
restaurantsRef50.child(key).once("value", function(snapshot) {
var restaurant50 = snapshot.val();
matches50.push(restaurant50);
for (var i = 0; i < matches50.length; i++)
if (matches50[i].uid && matches50[i].uid === userId) {
matches50.splice(i, 1);
break;
}
$timeout(function(){
//here we scope the array to write in our DOM
$scope.businessAd = matches50;
// Set a timeout to clear loader, however you would actually call the $ionicLoading.hide(); method whenever everything is ready or loaded.
$timeout(function () {
$ionicLoading.hide();
}, 1000);
})
});
});
//我们检查500米范围内的企业,以显示随机的宽范围广告
//首先我们得到用户的坐标
数据库.once('value',函数(快照){
var lat50=snapshot.val().lat;
var long50=snapshot.val().long;
var rootRef50=firebase.database().ref();
var geoFireRef50=rootRef50.child(“geobusiness”);
var restaurantsRef50=rootRef50.child(“businessarround”);
var geoFire50=新的GeoFire(geoFireRef50);
//然后我们打电话给Geofire检查周围的所有业务
var geoQuery50=geoFire50.query({
中间:[lat50,long50],
半径:0.5
});
//这是包含以下结果的数组
var matches50=[];
控制台日志(匹配50);
//倾听我们查询中的每一项业务。。。
geoQuery50.on(“输入键”),功能(键){
//…然后按键查找它们。。。
RestaurantRef50.child(键).一次(“值”),函数(快照){
var restaurant50=snapshot.val();
匹配50.推送(餐厅50);
对于(变量i=0;i
HTML代码:
<div class="content2" ng-repeat="businessAd in businessAds" ng-controller="businessPageController">
<div id="map"></div>
<div class="profile-info">
<!-- *profile-image / image profile -->
<img class="profile-image" src="img/100.jpg">
<!-- *profile-name / name profile -->
<h3 class="profile-name">{{businessAd.name}}</h3>
<!-- *profile-description / description profile -->
<span class="profile-description">
{{businessAd.description}}
</span>
<br /><br />
</div>
</div>
{{businessAd.name}
{{businessAd.description}
但是,现在,我想随机显示它们,因为它不是一个列表,而是一个单一的位置。因此,我想随机将数组中的一个对象显示到HTML代码中
此外,我想确保如果我的数组中只有一个对象,它总是显示它
怎么办?这听起来像是可以在控制器中实现的东西,首先想到的是这样的东西 控制器:
var matchLength = matches50.length;
var randInBound = Math.floor(Math.random() * matchLength) + 1;
$scope.matched = matches50[randInBound];
然后,从html中,您应该能够访问并显示该对象,并且只要数组中至少有一个项,它就会始终为您提供结果。这听起来像是您可以在控制器中实现的东西,首先想到的是这样的东西 控制器:
var matchLength = matches50.length;
var randInBound = Math.floor(Math.random() * matchLength) + 1;
$scope.matched = matches50[randInBound];
然后,从您的html中,您应该能够访问并显示此对象,并且只要数组中至少有1个项,它就会始终为您提供结果。我认为您不需要在此处重复ng,因为您只想显示一个项(如果没有项,则不显示),因此将ng repeat替换为:
ng-if="businessAd"
你可以把它放在你的控制器里,得到一个随机的加法
//here we scope the array to write in our DOM
var matches50Length = matches50.length;
if(matches50Length > 0){
// get random array index
var random = matches50[Math.floor(Math.random()* matches50.length)];
$scope.businessAd = random;
}
我认为这里不需要ng repeat,因为您只想显示一个项目(如果没有项目,则不显示),因此将ng repeat替换为:
ng-if="businessAd"
你可以把它放在你的控制器里,得到一个随机的加法
//here we scope the array to write in our DOM
var matches50Length = matches50.length;
if(matches50Length > 0){
// get random array index
var random = matches50[Math.floor(Math.random()* matches50.length)];
$scope.businessAd = random;
}
为什么不选择第一个元素呢?因为它在500米半径范围内显示企业的广告,我不想显示最近的一个,我想在这500米范围内随机选择。特别是因为之后我可以选择“频率”其中一些将在数组中多次出现…我不确定这是否可行,请尝试将Javascript日期对象与GetMillimes()一起使用那么%array.纵向上为什么不选择第一个元素呢?因为它在500米半径范围内显示企业的广告,我不想显示最近的一个,我希望在这500米范围内随机选择。特别是因为在这之后,我可以选择“频率”其中一些将在数组中多次出现…我不确定这是否可行,请尝试使用Javascript日期对象和GetMillimes()然后%array.Length。我认为这不会选择matches50[0],如果该值为空,则在选择matches50[1]时可能会失败。我认为Math.floor(Math.random()*MatchesLength)+1始终>=1感谢您的帮助,我发现另一个答案更适合我的情况!我还是投了赞成票!我认为这不会选择matches50[0],如果该选项为空,则在选择matches50[1]时可能会失败。我认为Math.floor(Math.random()*matchLength)+1始终>=1感谢您的帮助,我发现另一个答案更适合我的情况!我还是投了赞成票!您好,这可能是正确的方式,但它不会显示任何内容,但是我的数组中有对象被推送…实际上我有两个对象要推送,它们都在数组中被推送,但没有sn不显示它们中的任何一个…这是在您对控制器进行更改之前发生的吗?如果是,则是:$scope.businessAd[0]。name和$scope.businessAd[0]。description?首先,控制台.log(随机)触发4次,我不知道为什么,然后是businessAd[0]of name and desc返回:Uncaught TypeError:无法读取未定义的属性“0”。但是console.log(random)实际上显示了数组中的随机数据,因此它存在!看起来您对控制器进行了更改。是吗?您对视图进行了此更改吗?ng if=“businessAd”替换ng repeat=“businessAds中的businessAds”?是的,我实际上把它放在了正确的位置:您好,它可能在正确的方向上,但它没有显示任何内容,但是在我的数组中有对象被推送…实际上我有两个对象要推送,两个都是