2017-10-05 88 views
0

我从api接收电影类型的数字。这意味着,1 =动作,34 =戏剧,22 =恐怖......我想显示流派名称而不是数字。 movie.genre_ids的AngularJS用单词替换整数数组

{{::movie.genre_ids}} 

HTML输出:[1,34,22]

应该是这样的:恐怖动作戏......不带括号&号

电影有多个流派,使有工作了。

在此先感谢

+0

这似乎只是一个JavaScript问题。角度如何适应? –

+0

寻找一个有角度的解决方案 –

+0

你可以格式化你的输出,所以它更清楚,以及? – theGleep

回答

2

假设您有$范围是这样的:

$scope.genres = []; 
$scope.genres[1] = 'Horror'; 
$scope.genres[34] = 'Action'; 
$scope.genres[22] = 'Drama'; 

$scope.movies = [ 
    {title: 'IT', genres: [1,22]}, 
    {title: 'Alien', genres: [1,34]} 
] 

你可以在视图中像这样

<div ng-repeat="movie in movies"> 
    <b>{{movie.title}}</b> 
    <i ng-repeat="genre in movie.genres">{{($index > 0 ? ', ' : '') + genres[genre]}}</i> 
</div> 

为了得到这样的页面

上IT惊栗,剧情
外星人恐怖,动作

0

如果你能够映射格式化为下面的格式,

$scope.mapping = {"1": "action", "2": "horror"}; 

您可以使用下面的代码显示输出。


var app = angular.module('myApp', []); 
 

 
app.controller('MyController', function MyController($scope, $filter) { 
 
\t $scope.mapping = {"1": "action", "2": "horror"}; 
 
    $scope.api = [1,2]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-controller='MyController' ng-app="myApp"> 
 
    <span ng-repeat="x in api">{{mapping[x]}}&nbsp;</span> 
 
</div>


+0

我可以在我现有的主要ng-repeat电影列表中使用这个吗?我在哪里放置{{:: movie.genre_ids}}? –

+0

@KarlHusten是的,只要变量定义正确,如果您遇到问题,请提供示例数据的简单提示!喜欢这个? ':: mapping [movie.genre_ids]' –

+0

@KarlHusten检查我的上述评论 –

0

可以初始化对象的一个​​静态数组这样

var movie.genre=[ 
    {1:'Horror'}, 
    {34:'Action'}, 
    {22:'Drama'}, 
    {56:'Comedy'}, 
    ..... 
] 

在你的HTML中使用的角度:

<span ng-repeat="id in movie.genre_ids"> 
{{movie.genre[d]}} 
</span>