我想從AngularJS中的一個對象創建一個數組,用於ui-bootstrap的typeahead。 ('蝙蝠俠'),但我需要能夠結合幾個屬性來創建一個新的數組元素('蝙蝠俠(韋恩,布魯斯))。我可以從一個對象('蝙蝠俠'),但我需要能夠結合幾個屬性來創建一個新的數組元素('蝙蝠俠(韋恩,布魯斯))。我試過$scope.heroList = _.pluck($scope.heroes, 'name' + '(' + 'lname' + ', ' + 'fname' + ')');
,但得到了所有的空值。採多個屬性
這是plunker。
我需要一個最終的陣列看起來像這樣:
$scope.heroList = ['Batman (Wayne, Bruce]', 'Daredevil (Murdoch, Jack'), ... ];
這裏是我的javascript:
var app = angular.module('app',[]);
app.controller('MainController', function($scope){
$scope.heroes = [
{
id: 1,
name: 'Iron Man',
fname: 'Tony',
lname: 'Stark',
location: 'Stark Tower',
comic: 'Marvel'
},
{
id: 2,
name: 'Batman',
fname: 'Bruce',
lname: 'Wayne',
location: 'Bat Cave',
comic: 'DC'
},
{
id: 3,
name: 'Superman',
fname: 'Clark',
lname: 'Kent',
location: 'Metroplis',
comic: 'DC'
},
{
id: 1,
name: 'Daredevil',
fname: 'Jack',
lname: 'Murdock',
location: 'Court Room',
comic: 'Marvel'
},
{
id: 5,
name: 'Flash',
fname: 'Barry',
lname: 'Allen',
location: 'Speedline',
comic: 'DC'
},
{
id: 6,
name: 'Hulk',
fname: 'Bruce',
lname: 'Banner',
location: 'Labratory',
comic: 'Marvel'
},
{
id: 7,
name: 'Hawkeye',
fname: 'Clint',
lname: 'Barton',
location: 'Nest',
comic: 'Marvel'
},
{
id: 8,
name: 'Thor',
fname: 'Donald',
lname: 'Blake',
location: 'Asgard',
comic: 'Marvel'
}
];
$scope.heroList = _.pluck($scope.heroes, 'name');
//$scope.heroList = _.pluck($scope.heroes, 'name' + '(' + 'lname' + ', ' + 'fname' + ')');
}); // end MainController
我會使用'collect' /'map'來代替'pluck',因爲它更加規範,IMO。 – 2014-08-27 12:52:02