AngluarJS过滤表按用户ID排序

时间:2015-12-22 23:15:53

标签: angularjs angularjs-controller angularjs-factory

我尝试按用户ID过滤GET调用。我一直收到一条错误,指出userId未定义。我不完全确定如何正确设置我的工厂。我是否需要在工厂安装吸气剂和吸尘器?

厂:

angular.module('myApp').factory('homeFactory',['$http', function($http){
            return{

                // GETS current user that is logged in.
                getCurrentUser: function(){
                    return $http.get("user/getCurrent")
                },

                getAllReports: function(userId){
                    return $http.get('report/user/' + userId)
                }
            }
    }])

控制器:

angular.module('myApp').controller('homeController',
        ['$scope', '$compile', '$element', 'homeFactory','toastr', 
         function($scope, $compile, $element, homeFactory, toastr){

    // Retrieves the current user that is logged into the app.
    $scope.getCurrentUser = homeFactory.getCurrentUser().then(
    function(success){
        $scope.currentUser = success.data;
    },
    function(error){
        $scope.currentUser = error;
    });

    //Trying to get all reports by the userId
    $scope.getAllReports = homeFactory.getAllReports($scope.currentUser).then(
    function(success){
        $scope.reportName = success.data;
        console.log(reportName);
        },
    function(error){
        $scope.reportName = error;
    }); 

}])

非常感谢您提前寻求帮助!

2 个答案:

答案 0 :(得分:0)

我认为你应该把

function(success){
    $scope.currentUser = success.data;
}

在第一个承诺成功函数内:

public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;

        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridLayout.LayoutParams(120,120));
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setPadding(2, 2, 2, 2);
        }
        else
        {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

答案 1 :(得分:0)

$scope.getCurrentUser = homeFactory.getCurrentUser().then(
function(success){
    $scope.currentUser = success.data;
    console.log($scope.currentUser);
    $scope.getAllReports = homeFactory.getAllReports($scope.currentUser.userId).then(
            function(success){
                $scope.reportName = success.data;
                console.log()
                },
            function(error){
                $scope.reportName = error;
            }); 
},
function(error){
    $scope.currentUser = error;
});