如何使用angularjs在函数中获取$scope值

How to get a $scope value inside a function with angularjs

本文关键字:获取 scope 函数 何使用 angularjs      更新时间:2023-09-26

我有一个分页代码,我应该在同一个控制器内重复使用,并认为把它放在一个函数内。但它并没有像我预期的那样起作用。总是发送一个错误,说某些值是未定义的。

我如何做到这一点。

(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
            });

        };
    }
})();

根据上面的代码,错误是:angular.js:13642无法读取未定义属性'length'

那么我怎么才能做到这一点呢?我很感激你的帮助。由于

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一再试图帮助我。也谢谢大家的帮助。

这是代码。想分享学习

//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
});