1

此控制器从服务中获取类别列表(从 JSON 文件中获取),然后它应该只从列表中获取一个类别:

function CategoryCtrl($scope, Category, $stateParams, _) {
  $scope.categories = Category.query(function (categories) {
      $scope.category = _.where(categories, { id: $stateParams.category_id });
      console.log("sp.c_id: " + $stateParams.category_id);
      console.log("categories: " + categories);
      console.log("category: " + $scope.category);
  });

但是,这不起作用。控制台输出为:

sp.c_id: 1 
categories: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] 
category:  

如果我用数组索引切换下划线命令,如下所示:

$scope.category = categories[$stateParams.category_id];

我获得了一个类别(但错误的类别,因为 id <> index)。

下划线通过服务注入,如下所示:

angular.module('underscore', []).
    factory('_', function() {
    return window._;
    });

知道可能出了什么问题吗?

编辑:JSON文件

[{"id":1,"name":"Close Reading","created_at":"2013-09-11T00:19:00.906Z","updated_at":"2013-09-21T13:21:05.123Z","subtitle":"Deliberate, careful reading will improve students’ grasp of every text."},{"id":2,"name":"Choosing Complex Texts","created_at":"2013-09-11T00:20:26.072Z","updated_at":"2013-09-21T13:21:07.698Z","subtitle":"What should your students be reading?"},{"id":3,"name":"Writing \u0026 Language","created_at":"2013-09-11T00:20:31.219Z","updated_at":"2013-09-21T13:21:08.008Z","subtitle":"What are the foundations of good written communication?"},{"id":4,"name":"Vocabulary","created_at":"2013-09-11T00:20:52.209Z","updated_at":"2013-09-21T13:21:08.824Z","subtitle":"Discover ways to expand students’ vocabulary."},{"id":5,"name":"Speaking \u0026 Listening","created_at":"2013-09-11T00:20:59.205Z","updated_at":"2013-09-21T13:21:09.744Z","subtitle":"Improve communication skills in your classroom."},{"id":6,"name":"Media Literacy \u0026 Technology","created_at":"2013-09-11T00:21:04.671Z","updated_at":"2013-09-21T13:21:10.042Z","subtitle":"Explore and apply the latest trends in digital media."},{"id":7,"name":"Differentiation","created_at":"2013-09-11T00:21:09.644Z","updated_at":"2013-09-21T13:21:10.363Z","subtitle":"Different students have different needs."},{"id":8,"name":"Reading Support","created_at":"2013-09-11T00:21:18.683Z","updated_at":"2013-09-21T13:21:10.820Z","subtitle":"Enrich your students’ reading experience."},{"id":9,"name":"Engagement \u0026 Motivation","created_at":"2013-09-11T00:21:35.022Z","updated_at":"2013-09-21T13:21:11.766Z","subtitle":"What makes students thirsty for learning?"},{"id":10,"name":"Performance Task Assessment","created_at":"2013-09-11T00:21:39.589Z","updated_at":"2013-09-21T13:21:12.107Z","subtitle":"Prepare students for the next generation of assessment."}]
4

1 回答 1

3

这可能会发生,因为$stateParams.category_id它是一个字符串。您应该能够通过将其转换为整数来解决此问题:

$scope.category = _.where(categories, { id: parseInt($stateParams.category_id) });

请记住,where()方法将返回一个数组。如果要获取单个实例,请使用findWhere()方法。

$scope.category = _.findWhere(categories, { id: parseInt($stateParams.category_id) });
于 2013-09-21T22:49:30.933 回答