开发者问题收集

如何使用 angularjs 获取函数内的 $scope 值

2016-10-20
721

我有一个分页代码,我应该在同一个控制器中重复使用它,并考虑将其放在一个函数中。但它没有像我预期的那样工作。总是发送一个错误,说某个值未定义。

我该如何实现这一点。

(function () {
    'use strict';

    angular
        .module('efutures.hr.controllers.creation', [])
        .controller('UserCreateController', UserCreateController);

    UserCreateController.$inject = ['$scope', '$location', '$rootScope', '$http', 'deptService', 'DeptNameService','EmployeeService'];

    function UserCreateController($scope, $location, $rootScope, $http, deptService, DeptNameService, EmployeeService) {

        (function initController() {
            deptService.getdepts(function (res) {

                $scope.depts = JSON.parse(res.data);
            });

            EmployeeService.GetEmpDetails(function (res) {
                $scope.FilterDetails = JSON.parse(res.data);   //This is the variable that I need inside the function.            

                $scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE

            });
        })();

        $scope.reset = function () {

            $('#depts').val('').trigger('change.select2');
            EmployeeService.GetEmpDetails(function (res) {
                $scope.FilterDetails = JSON.parse(res.data);
            });
        };

        $scope.paginationTrigger =function(details){ //This method is used to control the pagination
             $scope.nums = ["10", "20", "30"];
                $scope.viewBy = $scope.nums[0];
                $scope.totalEmployees = $scope.details.length;
                $scope.currentPage = 1;
                $scope.itemsPerPage = $scope.viewBy;
                $scope.maxSize = 5;

                $scope.setPage = function (pageNo) {
                    $scope.currentPage = pageNo;
                };

                $scope.pageChanged = function () {
                    console.log('Page changed to: ' + $scope.currentPage);
                }

                $scope.setEmployeesPerPage = function (num) {
                    $scope.itemsPerPage = num;
                    $scope.currentPage = 1;
                }
        }

        $scope.DropdownSelected = function (value) {
            console.log(value.DeptName + ' Selected');

            var DeptNameChanged = {
                'Name': value
            };

            DeptNameService.DeptNameValue(DeptNameChanged, function (res) {

                $scope.FilterDetails = JSON.parse(res.data);

                $scope.PaginationTrigger($scope.FilterDetails); //CODE APPLIED HERE

            });


        };
    }
})();

According to the above code the ERROR IS: angular.js:13642 TypeError: Cannot read property 'length' of undefined

那么我该如何实现呢?如能得到帮助我将不胜感激。谢谢

2个回答
test = {"a" : 1}
details = "a"
alert(test[details])

使用 $scope[details].length ,因为 details 是那里的一个参数。

$scope.paginationTrigger =function(details){ //This method is used to control the pagination
         $scope.nums = ["10", "20", "30"];
            $scope.viewBy = $scope.nums[0];
            $scope.totalEmployees = $scope[details].length;
            $scope.currentPage = 1;
            $scope.itemsPerPage = $scope.viewBy;
            $scope.maxSize = 5;

            $scope.setPage = function (pageNo) {
                $scope.currentPage = pageNo;
            };

            $scope.pageChanged = function () {
                console.log('Page changed to: ' + $scope.currentPage);
            }

            $scope.setEmployeesPerPage = function (num) {
                $scope.itemsPerPage = num;
                $scope.currentPage = 1;
            }
    }

请查看代码片段,您将收到错误。

Sravan
2016-10-20

我终于搞明白了。这就是我试图完成的,而且成功了。特别感谢@Sravan 多次试图帮助我。也感谢大家的帮助。

这是代码。想分享以供学习。

//Create a function in the controller
function paginationTrigger(value) {

    $scope.nums = ["10", "20", "30"];
    $scope.viewBy = $scope.nums[0];
    $scope.totalEmployees = value.length;
    $scope.currentPage = 1;
    $scope.itemsPerPage = $scope.viewBy;
    $scope.maxSize = 5;

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.pageChanged = function () {
        console.log('Page changed to: ' + $scope.currentPage);
    }

    $scope.setEmployeesPerPage = function (num) {
        $scope.itemsPerPage = num;
        $scope.currentPage = 1;
    }
}

/* Call the function in the desired palce.
In this case inside my service function  */

EmployeeService.GetEmpDetails(function (res) {
    $scope.FilterDetails = JSON.parse(res.data);
    //pagination code
    paginationTrigger($scope.FilterDetails); //Passing the value

});
Ashane Alvis
2016-10-21