Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 从列表中随机抽取一项_Angularjs - Fatal编程技术网

Angularjs 从列表中随机抽取一项

Angularjs 从列表中随机抽取一项,angularjs,Angularjs,我得到了一个项目列表,并循环了一个类似这样的html代码。(粘贴的html片段不完整,因为它很长) {{recentuseraddress.updated_time}}{{recentuseraddress.recent_connect_address}{{recentuseraddress.recent_connect_postcode} ...... 你可以在html中看到图像标签,基本上它会打印我列表中的所有图像 现在在同一页面的另一个地方,我只需要从同一个角度随机获取一张图片 是否

我得到了一个项目列表,并循环了一个类似这样的html代码。(粘贴的html片段不完整,因为它很长)



{{recentuseraddress.updated_time}}


{{recentuseraddress.recent_connect_address}{{recentuseraddress.recent_connect_postcode}

......
你可以在html中看到图像标签,基本上它会打印我列表中的所有图像

现在在同一页面的另一个地方,我只需要从同一个角度随机获取一张图片

是否有任何直接方法可以从列表中随机提取项目

多谢各位

使用:

$scope.randomImage= $scope.recentuseraddress.propertyimg[Math.floor(Math.random() * $scope.recentuseraddress.propertyimg.length)];
在视图中:{{randomImage}使用:

$scope.randomImage= $scope.recentuseraddress.propertyimg[Math.floor(Math.random() * $scope.recentuseraddress.propertyimg.length)];

在视图中:{{randomImage}

您应该从控制器中的时间线中选择随机元素。 我根据您的代码片段模拟了您的情况,并编写了这个示例

angular.module('app',[])
.controller('mainController',函数($scope){
var时间线=[
{home_id:1,propertyimg:'img1.jpg'},
{home_id:2,propertyimg:'img2.jpg'},
{home_id:3,propertyimg:'img3.jpg'}
];
$scope.timeline=时间线;
$scope.randomRecentUserAddress=timeline[Math.floor(Math.random()*timeline.length)];
});

JS-Bin
时间线
{{时间线}}
随机的
{{randomRecentUserAddress}

您应该从控制器中的时间线中选择随机元素。 我根据您的代码片段模拟了您的情况,并编写了这个示例

angular.module('app',[])
.controller('mainController',函数($scope){
var时间线=[
{home_id:1,propertyimg:'img1.jpg'},
{home_id:2,propertyimg:'img2.jpg'},
{home_id:3,propertyimg:'img3.jpg'}
];
$scope.timeline=时间线;
$scope.randomRecentUserAddress=timeline[Math.floor(Math.random()*timeline.length)];
});

JS-Bin
时间线
{{时间线}}
随机的
{{randomRecentUserAddress}

非常感谢。只是看到了一种可能性,比如说列表是空的,没有什么可以随机选择的,在这种情况下,有没有一种方法可以在VIEW中解决这个问题我在手机上。所以答案不会很详细。您只需检查列表中的空条件,并使用一些语句分配一个范围变量,然后在Handlebar的帮助下在视图中显示它。非常感谢。这有助于我们必须勾选正确的答案。如果这个答案对你有帮助,老兄。非常感谢。只是看到了一种可能性,比如说列表是空的,没有什么可以随机选择的,在这种情况下,有没有一种方法可以在VIEW中解决这个问题我在手机上。所以答案不会很详细。您只需检查列表中的空条件,并使用一些语句分配一个范围变量,然后在Handlebar的帮助下在视图中显示它。非常感谢。这有助于我们必须勾选正确的答案。如果答案对你有帮助,老兄。非常感谢。这也很有帮助。非常感谢。这也很有帮助