Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度类别排序不按类别名称工作_Javascript_Angularjs - Fatal编程技术网

Javascript 角度类别排序不按类别名称工作

Javascript 角度类别排序不按类别名称工作,javascript,angularjs,Javascript,Angularjs,我试图在Angular js中实现类别排序,但它不能正常工作。它还从描述中提取数据。我想限制它的类别名称,但没有运气 JS代码: var myApp = angular.module('myApp', []); myApp.factory('Items', ['$http', function($http){ return { get: function(callback){ $http.get('items.json').success(function(data){

我试图在Angular js中实现类别排序,但它不能正常工作。它还从描述中提取数据。我想限制它的类别名称,但没有运气

JS代码:

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

myApp.factory('Items', ['$http', function($http){
  return {
    get: function(callback){
      $http.get('items.json').success(function(data){
        callback(data);
      })
    }
  }
}]);

myApp.factory('Categories', ['$http', function($http){
  return {
    get: function(callback){
      $http.get('categories.json').success(function(data){
        callback(data);
      })
    }
  }
}]);

// Config and Routes 
myApp.config(function($routeProvider){
  $routeProvider
        .when('/', {
                templateUrl:"home.html"
        })
        .when('/item/:id', {
                templateUrl:"item.html"
        })
})

myApp.controller('headerController', function($scope, $location) {
  $scope.goHome = function () {
    $location.path('/');
  };
})



function Ctrl($scope) {
  $scope.greeting = 'hello';
}

// Controllers
myApp.controller('ItemController', function($scope, $route, $location, $http, Items){

  Items.get(function(response){
    $scope.items = response;
  });

  // Update this value dynamically - onclick
  $scope.filters = "food";

  $scope.viewDetail = function(item) {
        $location.path('/item/' + item.id);
    }

})  


myApp.controller('ListController', function($scope, $route, $location, $http, Categories){

  $scope.sendCategory = function(category) {
    // How can I pass this value to ItemController?
     $scope.search =category.name;
  };

   $scope.orderProp='date';

    $scope.tab = function (tabIndex) {
     //Sort by date
      if (tabIndex == 1){
        //alert(tabIndex);
        $scope.orderProp='date';

      }   
      //Sort by views 
      if (tabIndex == 2){
        $scope.orderProp = 'views';
      }

   };

   $scope.sort = function(item) {
       if (  $scope.orderProp == 'date') {
            return new Date(item.date);
        }
        return item[$scope.orderProp];
   }

})  


myApp.controller('CategoryController', function($scope, $route, $location, $http, Categories){

  Categories.get(function(response){
    $scope.categories = response;
  });


})  

myApp.controller("tabsController", function ($scope) {

   $scope.orderProp = 'date';

})

myApp.controller('ItemDetailController', function($scope, $route, $location, $http, Items){
    Items.get(function(response){
    $scope.items = response; 

    if ($route.current.params.id) {
      angular.forEach($scope.items, function (v, k) {
        if (v.id == $route.current.params.id) {
          $scope.currItem = $scope.items[k];
          return false;
        }
      });
    }
  });
})
有谁能帮我找到一种方法,我只能根据我的类别名称对数据进行排序,而不是在整个页面上搜索数据

共享我为此找到的参考URL


如果有人能帮我的话,那就太好了。

你也能提供html吗?因为我猜你正在使用一个角度过滤器来实现这一点。。。顺便说一句,Items服务只是一个围绕$http的限制性包装。相反,为什么不为'items.json'路径创建一个常量并直接使用$http呢?这样,您就可以使用所有的Promise函数(即添加错误处理程序或finally()回调)……嗨,Shaun,如果您可以编辑我的示例URL,将对我有很大帮助。我不确定你的指导。谢谢你抽出时间。