自动完成所需的 AngularJS 多个范围

标签 angularjs angularjs-scope angularjs-controller angularjs-filter

我有一个这样的数组结构。

[
  {
    "id": "1",
    "name": "John",
    "city": "NY"
  },
  {
    "id": "2",
    "name": "Gerold",
    "city": "LA"
  },
  {
    "id": "3",
    "name": "Stuart",
    "city": "Boston"
  }  
]

我需要像下面这样的 $scope 来进行自动完成搜索。

$scope.name=["john","Gerold","Stuart"];
$scope.city=["NY","LA","Boston"];

任何人都可以使用 angularjs Controller 帮助获得它。 提前致谢!

最佳答案

使用 map

$scope.users = [
  {
  "id": "1",
  "name": "John",
  "city": "NY"
  },
 {
  "id": "2",
  "name": "Gerold",
  "city": "LA"
 },
 {
   "id": "3",
  "name": "Stuart",
  "city": "Boston"
 }  
];

$scope.cities = $scope.users.map(function(obj){ 

 return obj.city;
});

console.log($scope.cities);

关于自动完成所需的 AngularJS 多个范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41016988/

相关文章:

javascript - Firebase/AngularFire - 是否需要身份验证、Login.Logoff?

javascript - 在 Angular js中获取选定的值

javascript - 如何轻松地与父范围通信

javascript - Angular : How can we broadcast promise to different controllers?

angularjs - 服务对象更新时 $watch 不更新

javascript - AngularJS ui-bootstrap 打开模态窗口而不改变url

javascript - 使用 length 函数返回 undefined

javascript - 使用 Ng-Required 进行 Angular 表单验证

AngularJS 结合使用两个具有共享范围的自定义指令

javascript - 如何从不同位置获得良好性能的 i18n?