简体   繁体   English

从范围数组获取唯一项

[英]Get unique item from scope array

I have an array with JSON objects in my $scope.users which is working correctly. 我的$ scope.users中有一个带有JSON对象的数组,它可以正常工作。 I'm now trying to set up a "detail" page for an individual user by filtering the users list to get a single user. 我现在正尝试通过过滤用户列表以获取单个用户来为单个用户设置“详细”页面。

I've been close for an excruciating amount of time, and for some reason I can't find documentation or any examples of how to do what I'm doing. 我已经关闭了很长时间,由于某种原因,我找不到文档或任何有关如何做自己的事的示例。 Maybe this isn't the correct way to do this in Angular and/or maybe I'm not searching for the right thing? 也许这不是在Angular中执行此操作的正确方法,并且/或者我没有在寻找正确的东西?

I've tried getting the object based on userId from the list in the controller, and I've tried passing a resolve in the state, but I didn't get either method to work. 我尝试从控制器的列表中获取基于userId的对象,并尝试在状态中传递解析,但没有任何一种方法可以工作。 What is the best way to go about getting the single user with id of $stateparams.userId? 获得ID为$ stateparams.userId的单个用户的最佳方法是什么?

Everything else is working correctly (ie all the routing, getting the users on #/users). 其他所有东西都正常工作(即所有路由,使用户进入#/ users)。

// routing section of app.js
// Routing
  $urlRouterProvider.otherwise('/');
  $stateProvider
    .state('home', {
      url: '/',
      templateUrl: 'static/partials/main/home.html'
    })
    .state('users', {
      url: '/users',
      templateUrl: 'static/partials/accounts/users.html',
      controller: 'UserController'
    })
    .state('profile', {
      url: '/users/{userId}',
      templateUrl: 'static/partials/accounts/profile.html',
      controller: 'UserController'
    });


// controller.js
var rlGlobalControllers = angular.module('rlGlobalApp.controllers', []);

rlGlobalControllers.controller('UserController', function($scope, $stateParams, $filter) {
  var userId = $stateParams.userId;

  $scope.users = [
    {
      'id': 1,
      'name': 'Shadrack'
    },
    {
      'id': 2,
      'name': 'JP'
    },
    {
      'id': 3,
      'name': 'Adam'
    }
  ];

  // $scope.user = ??? 
});

# profile.html
<div ng-include src="'/static/partials/shared/header.html'"></div>

<div class="container">
  <div class="row">
    <p>users data: {{ users }}</p>
    <p>user data: {{ user }}</p>
  </div>
</div>

User Array.prototype.filter method to find objects satisfying criteria: 用户Array.prototype.filter方法可查找满足条件的对象:

$scope.user = $scope.users.filter(function(user) {
    return user.id === userId;
})[0];

This is the most natural way to solve it. 这是最自然的解决方法。 If however your users array is very large (like millions, for example (hope it's not)) then for-loop-break solution of juunas is preffered as more effective. 但是,如果您的users数组非常大(例如,数百万(希望不是)),那么juunas的for-loop-break解决方案被认为更有效。

You could just do this in the controller: 您可以在控制器中执行以下操作:

for(var i = 0; i < $scope.users.length; i++){
  if($scope.users[i].id === userId){
    $scope.user = $scope.users[i];
    break;
  }
}
var output = [],
            keys = [];
          Array_Value.forEach(function (item) {
            var key = item["Field name"];
            if (keys.indexOf(key) === -1) {
              keys.push(key);
              output.push(item);
            }
          });
          this.Title = output;

Instead of Array_value and field name, give your data. 提供您的数据,而不是Array_value和字段名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM